HttpClient介绍

来源:互联网 发布:网络招商推广语言 编辑:程序博客网 时间:2024/05/22 12:23

一、HttpClient介绍
 
HttpClient是用来模拟HTTP请求的,其实实质就是把HTTP请求模拟后发给Web服务器;
 
Android已经集成了HttpClient,因此可以直接使用;
 
注:此处HttpClient代码不只可以适用于Android,也可适用于一般的Java程序;
 
HTTP GET核心代码:
 
 (1)DefaultHttpClient client = new DefaultHttpClient();
 (2)HttpGet get = new HttpGet(String url);//此处的URL为
http://..../path?arg1=value&....argn=value
 (3)HttpResponse response = client.execute(get); //模拟请求
 (4)int code = response.getStatusLine().getStatusCode();//返回响应码
 (5)InputStream in = response.getEntity().getContent();//服务器返回的数据
 

HTTP POST核心代码:
 
 (1)DefaultHttpClient client = new DefaultHttpClient();
 (2)BasicNameValuePair pair = new BasicNameValuePair(String name,String value);//创建一个请求头的字段,比如content-type,text/plain
 (3)UrlEncodedFormEntity entity = new UrlEncodedFormEntity(List<NameValuePair> list,String encoding);//对自定义请求头进行URL编码
 (4)HttpPost post = new HttpPost(String url);//此处的URL为
http://..../path
 (5)post.setEntity(entity);
 (6)HttpResponse response = client.execute(post);
 (7)int code = response.getStatusLine().getStatusCode();
 (8)InputStream in = response.getEntity().getContent();//服务器返回的数据

其中

UrlEncodedFormEntity,直接继承自StringEntity

有两个构造方法:

1. UrlEncodedFormEntity(List<? extends NameValuePair> parameters, String encoding)

2. UrlEncodedFormEntity(List<? extends NameValuePair> parameters)

第二个方法将使用默认编码,即ISO-8859-1

例如:

/*
     * NameValuePair代表一个HEADER,List<NameValuePair>存储全部的头字段
     * UrlEncodedFormEntity类似于URLEncoder语句进行URL编码
     * HttpPost类似于HTTP的POST请求
     * client.execute()类似于发出请求,并返回Response
     */
 public static String AccessWebUtil(HashMap<String, String> param, String url) {

  String content = "";
  HttpPost request = new HttpPost(url);
  List<NameValuePair> paramers = new LinkedList<NameValuePair>();
  for (Map.Entry<String, String> entry : param.entrySet()) {
   paramers.add(new BasicNameValuePair(entry.getKey(), entry
     .getValue()));
  }
  UrlEncodedFormEntity entity = null;
  try {
   entity = new UrlEncodedFormEntity(paramers, "utf-8");
  } catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
  }
  BasicHttpParams httpParams = new BasicHttpParams();
  HttpConnectionParams.setConnectionTimeout(httpParams, 1000 * 60);
  HttpConnectionParams.setSoTimeout(httpParams, 1000 * 60);
  request.setEntity(entity);
  HttpClient httpClient = new DefaultHttpClient(httpParams);
  BufferedReader in = null;
  try {
   // 执行请求参数项
   HttpResponse response = httpClient.execute(request);
   // 判断是否请求成功
   // System.out.println("temp=="+response.getStatusLine().getStatusCode()
   // );
   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    // 获得响应信息
    in = new BufferedReader(new InputStreamReader(response
      .getEntity().getContent()));

    StringBuffer string = new StringBuffer("");
    String lineStr = "";
    while ((lineStr = in.readLine()) != null) {
     string.append(lineStr + "\n");
    }

    content = string.toString();
    return content;
   } else {

   }

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (in != null) {
    try {
     in.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   // 释放网络连接资源
   httpClient.getConnectionManager().shutdown();
  }
  return "";

 }

 

0 0