HttpClient的用法总结

使用HttpClient连接服务端的步骤:

1.创建HttpClient客户端对象
HttpClient client = new DefaultHttpClient();
2.创建请求对象
         1.1 GET请求
HttpUriRequest request = new HttpGet(String uri)
                   HttpUriRequest request = new HttpGet(URI uri)
         1.2 POST请求
                   1.2.1  创建Post方法的请求对象
                   HttpUriRequest request = new HttpPost()
                   HttpUriRequest request = new HttpPost(String uri)
                   HttpUriRequest request = new HttpPost(URI uri)
                   1.2.2  创建请求参数的List集合
                   List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                   pairs.add(new BasicNameValuePair(“name” , “value”));
                   ...
                   1.2.3  使用请求参数List集合创建并设置请求实体
                   UrlEncodedFormEntity requestEntity = new UrlEncodedFormEntity(pairs);
                   (HttpPost)request.setEntity(requsetEntity);  //setEntityHttpPost特有的方法
3.客户端执行响应
         HttpResponse response = client.execute(request);
4.获取并判断响应码,获取响应实体对象
         if(response.getStatusLine().getStatusCode == HttpStatus.SC_OK){
                  HttpEntity responseEntity = response.getEntity();
         }
5.解析响应实体对象
         InputStream is = entity.getContent();     //获取内容读取流
         EntityUtils.toByteArray(entity);                //转换成字节数组
         EntityUtils.toString(entity);                        //转换成字符串
         EntityUtils.toString(entity,"utf-8");                   //使用相应编码转换成字符串

开发中的模式

public class HttpUtils{
         
         //设置请求类型常量
         public static final int METHOD_GET = 1;
         public static final int METHOD _POST = 2;
         /*
          *创建方法获取响应实体
          *需要知道的参数有三个
          *URI 带有文件名称的整个网络地址
          *List<NameValuePair> 带有参数的键值对
          *int 访问的方法,如果是1,代表GET方法;如果是2,代表POST方法
         */
         public HttpEntity getEntity(String uri , List<NameValuePair> params , int method)
throws Exception{
         //创建HttpEntity的引用
         HttpEntity responseEntity = null;
         //创建客户端对象
         HttpClient client = new DefaultHttpClient();
         //创建请求对象
         HttpUriRquest request = null;
         //判断方法时GET还是POST,并且创建请求对象
         switch(method){
         case 1:
                   StringBuilder sb = new StringBuilder(uri);
                   //循环加入请求参数
                   if(params != null && !params.isEmpty()){
         sb.append(“?”);
         for(NameValuePair pair : pairs){
                   sb.append(pair.getName());
                   sb.append(“=”);
                   sb.append(pair.getValue());
                   sb.append(“&”);
}
sb.deleteCharAt(sb.lenght()-1);
}
request = new HttpGet(sb.toString());
break;
                   case 2:
                            request = HttpPost(uri);
                            if(params != null && !params.isEmpty()){
                                     //创建请求实体
                                     UrlEncodedFormEntity requestEntity = new UtlEncodedFormEntity(params);
                                     ((HttpPost)request).setEntity(requestEntity);
                            }
                            break;
}
//执行请求,获取相应对象
HttpResponse response = client.execute(request);
//获取响应实体
if(response.getStatusLine().getStatusCode() == 200){
responseEntity = response.getEntity();
}
return responseEntity;
}
}

HttpClient中常用到的类

HttpClient
         |-- DefaultHttpClient
                   构造方法:     DefaultHttpClient
                   主要方法:     HttpResponse  execute(HttpUriRequest request)
HttpUriRequest
         |-- HttpGet
                   构造方法:     HttpGet()
                                               HttpGet(String uri)
         |-- HttpPost
                   构造方法:     HttpPost(String uri)
                   主要方法:     void setEntity(HttpEntity entity)
HttpResponse
         主要方法:
                   StatusLine getStatusLine()
                   Header[] getAllHeaders();
                   HttpEntity getEntity();
HttpEntity
         主要方法:
                   InputStream getContent();
                   long getContentLength();
                   Header getContentType();
         |-- UrlEncodedFormEntity
                   构造方法:UrlEncodedFormEntity(List<? extends NameValuePair> params)
                                               //用于向请求对象中写入请求实体(包含请求参数(NameValuePair))
EntityUtils
         public static byte[] toByteArray(HttpEntity entity)
         public static String toString(HttpEntity entity)
         public static String toString(HttpEntity entity , String encoding)
StatusLine
         int getStatusCode()
HttpStatus
         SC_OK   SC_NOT_FOUND
Header
         String getName()
         String getValue()
NameValuePair
         String getName()
         String getValue()
         |-- BasicNameValuePair
                   构造方法:BasicNameValuePair(String name , String value)

快速记忆(POST

1.       要想连接网络,肯定需要一个客户端对象,所以首先创建一个客户端对象HttpClient
2.       拥有客户端对象之后肯定需要对互联网进行访问,首先要对互联网进行请求,所以要创建一个请求对象HttpUriRequest,因为是Post请求,所以使用HttpPost
3.       我们知道请求对象包含请求行(方法、路径、协议),消息头,请求实体三部分。通过new HttpPost(uri)可以设置请求行的路径,现在需要设置请求实体中的请求参数。
我们知道请求参数一定是由请求参数的名称和请求参数的值组成。这时就需要一个类NameValuePair,这是一个键值对,在初始化的时候为了简便直接使用其子类BaseNameValuePair(String name , String value)
如果有多组请求参数就要使用List集合List<NameValuePair>
4.       已经有一个存放参数的键值对的集合只有,就可以创建一个请求实体对象,并且将参数集合传进去—new UrlDecodedFormEntity(List<NameValuePair>)
5.       然后设置请求对象的请求实体
request.setEntity(new UrlDecodedFormEntity(List<NameValuePair>))
6.       现在不管是要访问的网络地址还是参数就都已经放入请求对象中了,现在就可以利用客户端进行请求将请求对象发送出去,并且获得响应对象
响应对象 = 客户端对象.execute(请求对象)
7.       我们都知道响应对象也是由状态行(协议,状态码,状态描述),消息头,响应实体三部分组成。现在有了响应对象就可以获取响应对象中的消息。
具体方法见上。
原文地址:https://www.cnblogs.com/Free-Thinker/p/3263787.html