Android客户端与服务器通信之HttpClient的使用

下面就总结下两种发送带参数的http请求:

两种方法的主要区别是如何携带参数(也就是键值对)的问题,get方法是把要传递的参数直接封装在url中(get方法中的url=要访问的服务器网址信息+需要传递的参数),而post方法是把传递的参数封装在一个list 对象里,然后把这个装有键值对的对象直接绑在httpPost对象上,不是放在url中。

1 package com.test.http; 2 3 import java.util.List; 4 5 import org.apache.http.HttpResponse; 6 import org.apache.http.HttpStatus; 7 import org.apache.http.NameValuePair; 8 import org.apache.http.client.HttpClient; 9 import org.apache.http.client.entity.UrlEncodedFormEntity; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.impl.client.DefaultHttpClient; 13 import org.apache.http.params.BasicHttpParams; 14 import org.apache.http.params.HttpConnectionParams; 15 import org.apache.http.params.HttpParams; 16 import org.apache.http.protocol.HTTP; 17 import org.apache.http.util.EntityUtils; 18 19 /** 20 *以同步方式发送Http请求 21 */ 22 public class HttpTools 23 { 24 25 /** 26 * 通过GET方式发送请求 27 * @param url URL地址 28 * @param params 参数 29 * @return 30 * @throws Exception 31 */ 32 public String getResultForHttpGet(String url, String params) throws Exception 33 { 34 String response = null; //返回信息 35 //拼接请求URL 36 if (null!=params&&!params.equals("")) 37 { 38 url += "?" + params; 39 } 40 41 int timeoutConnection = 10000000; 42 int timeoutSocket = 5000000; 43 //设置网络链接超时 44 HttpParams httpParameters = new BasicHttpParams(); 45 //设置socket响应超时 46 HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 47 HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 48 49 // 构造HttpClient的实例 50 HttpClient httpClient = new DefaultHttpClient(httpParameters); 51 52 System.out.println("URL = "+url); 53 // 生成一个请求对象 54 HttpGet httpGet = new HttpGet(url); 55 56 try 57 { 58 // 使用Http客户端发送请求对象 59 HttpResponse httpResponse = httpClient.execute(httpGet); 60 61 int statusCode = httpResponse.getStatusLine().getStatusCode(); 62 if (statusCode == HttpStatus.SC_OK) //SC_OK = 200 63 { 64 // 获得返回结果 65 response = EntityUtils.toString(httpResponse.getEntity()); 66 } 67 else 68 { 69 response = "返回码:" + statusCode; 70 } 71 } catch (Exception e) 72 { 73 throw new Exception(e); 74 } 75 76 return response; 77 } 78 79 /** 80 * 通过POST方式发送请求 81 * @param url URL地址 82 * @param params 参数 83 * @return 84 * @throws Exception 85 */ 86 public String getReultForHttpPost(String url, List<NameValuePair> params) throws Exception 87 { 88 String response = null; 89 int timeoutConnection = 300000; 90 int timeoutSocket = 500000; 91 // 设置网络链接超时 92 HttpParams httpParameters = new BasicHttpParams(); 93 // 设置socket响应超时 94 HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 95 HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 96 // 构造HttpClient的实例 97 HttpClient httpClient = new DefaultHttpClient(httpParameters); 98 HttpPost httpPost = new HttpPost(url); 99 if (params.size()>=0) 100 { 101 //设置httpPost请求参数 102 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 103 } 104 //使用execute方法发送HTTP Post请求,并返回HttpResponse对象 105 HttpResponse httpResponse = httpClient.execute(httpPost); 106 int statusCode = httpResponse.getStatusLine().getStatusCode(); 107 if(statusCode == HttpStatus.SC_OK) 108 { 109 //获得返回结果 110 System.out.println("Post请求成功"); 111 response = EntityUtils.toString(httpResponse.getEntity()); 112 } 113 else 114 { 115 response = "返回码:"+statusCode; 116 } 117 return response; 118 } 119 120 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyspfs.html