码迷,mamicode.com
首页 > Web开发 > 详细

httpclient_1

时间:2014-12-16 00:45:57      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   os   sp   for   

bubuko.com,布布扣

package com.httpclient;

/**
* http://api.xxx.com/api/android/
* commandid=guestpacklist&coursetype=6&grade=1039&mac=78%3Af5%3Afd%3A7b%3Ac2%3A9d&pagenumber=1&phpsessid=&rowcount=8&signed=0aa38c77108b23f4e1b588ac02338445&subject=1001&subversion=8014&userid=
*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class CustomHttpClient {
    public void postForm() {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost("http://api.xxx.com/api/android/");
        // 创建参数队列
        ArrayList<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
        formparams.add(new BasicNameValuePair("commandid", "guestpacklist"));
        formparams.add(new BasicNameValuePair("coursetype", "6"));
        formparams.add(new BasicNameValuePair("grade", "1039"));
        formparams.add(new BasicNameValuePair("mac",
                "78%3Af5%3Afd%3A7b%3Ac2%3A9d"));
        formparams.add(new BasicNameValuePair("pagenumber", "1"));
        formparams.add(new BasicNameValuePair("phpsessid", "6"));
        formparams.add(new BasicNameValuePair("rowcount", "8"));
        formparams.add(new BasicNameValuePair("signed",
                "0aa38c77108b23f4e1b588ac02338445"));
        formparams.add(new BasicNameValuePair("subject", "1001"));
        formparams.add(new BasicNameValuePair("subversion", "8014"));
        formparams.add(new BasicNameValuePair("userid", ""));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out
                            .println("--------------------------------------");
                    System.out.println("Response content: "
                            + EntityUtils.toString(entity, "UTF-8"));
                    System.out
                            .println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        CustomHttpClient chttpclient=new CustomHttpClient();
        chttpclient.postForm();
    }
}

  • public void get() { 
  •         CloseableHttpClient httpclient = HttpClients.createDefault(); 
  • try { 
  • // 创建httpget. 
  •             HttpGet httpget = new HttpGet("http://www.baidu.com/"); 
  •             System.out.println("executing request " + httpget.getURI()); 
  • // 执行get请求. 
  •             CloseableHttpResponse response = httpclient.execute(httpget); 
  • try { 
  • // 获取响应实体 
  •                 HttpEntity entity = response.getEntity(); 
  •                 System.out.println("--------------------------------------"); 
  • // 打印响应状态 
  •                 System.out.println(response.getStatusLine()); 
  • if (entity != null) { 
  • // 打印响应内容长度 
  •                     System.out.println("Response content length: " + entity.getContentLength()); 
  • // 打印响应内容 
  •                     System.out.println("Response content: " + EntityUtils.toString(entity)); 
  •                 } 
  •                 System.out.println("------------------------------------"); 
  •             } finally { 
  •                 response.close(); 
  •             } 
  •         } catch (ClientProtocolException e) { 
  •             e.printStackTrace(); 
  •         } catch (ParseException e) { 
  •             e.printStackTrace(); 
  •         } catch (IOException e) { 
  •             e.printStackTrace(); 
  •         } finally { 
  • // 关闭连接,释放资源 
  • try { 
  •                 httpclient.close(); 
  •             } catch (IOException e) { 
  •                 e.printStackTrace(); 
  •             } 
  •         } 
  •     } 

httpclient_1

标签:android   style   blog   http   io   ar   os   sp   for   

原文地址:http://www.cnblogs.com/stay-sober/p/4166115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!