Android SDK集成了Apache HttpClient模塊。要注意的是,這裏的Apache HttpClient模塊是HttpClient 4.0(org.apache.http.*),而不是常見的Jakarta Commons HttpClient 3.x(org.apache.commons.httpclient.*)。
HttpClient常用HttpGet和HttpPost這兩個類,分別對應Get方式和Post方式。
無論是使用HttpGet,還是使用HttpPost,都必須通過如下3步來訪問HTTP資源。
1.創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。
2.使用DefaultHttpClient類的execute方法發送HTTP GET或HTTP POST請求,並返回HttpResponse對象。
3.通過HttpResponse接口的getEntity方法返回響應信息,並進行相應的處理。
如果使用HttpPost方法提交HTTP POST請求,則需要使用HttpPost類的setEntity方法設置請求參數。參數則必須用NameValuePair[]數組存儲。
HttpGet
- public String doGet()
- {
- String uriAPI = "http://XXXXX?str=I+am+get+String";
- String result= "";
- // HttpGet httpRequst = new HttpGet(URI uri);
- // HttpGet httpRequst = new HttpGet(String uri);
- // 創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。
- HttpGet httpRequst = new HttpGet(uriAPI);
- // new DefaultHttpClient().execute(HttpUriRequst requst);
- try {
- //使用DefaultHttpClient類的execute方法發送HTTP GET請求,並返回HttpResponse對象。
- HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);//其中HttpGet是HttpUriRequst的子類
- if(httpResponse.getStatusLine().getStatusCode() == 200)
- {
- HttpEntity httpEntity = httpResponse.getEntity();
- result = EntityUtils.toString(httpEntity);//取出應答字符串
- // 一般來說都要刪除多餘的字符
- result.replaceAll("\r", "");//去掉返回結果中的"\r"字符,否則會在結果字符串後面顯示一個小方格
- }
- else
- httpRequst.abort();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- }
- return result;
- }
cookie等參數的設置
- //設置httpGet的參數信息
- httpGet.setHeader("Accept", "Accept text/html,application/xhtml
- +xml,application/xml;q=0.9,*/*;q=0.8");
- httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
- httpGet.setHeader("Accept-Encoding", "gzip, deflate");
- httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
- httpGet.setHeader("Connection", "keep-alive");
- httpGet.setHeader("Cookie",
- "__utma=226521935.73826752.1323672782.1325068020.1328770420.6;");
- httpGet.setHeader("Host", "www.cnblogs.com");
- httpGet.setHeader("refer", "http://www.baidu.com/s?
- tn=monline_5_dg&bs=httpclient4+MultiThreadedHttpConnectionManager");
- httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2)
- Gecko/20100101 Firefox/6.0.2");
HttpPost
如果使用HttpPost方法提交HTTP POST請求,則需要使用HttpPost類的setEntity方法設置請求參數。參數則必須用NameValuePair[]數組存儲。或者用
參數=值&參數=值字符串的類型
數組參數類型----如果數組類型返回302 則改用 字符串參數類型
字符串參數類型
- String result = "";
- HttpPost httpRequst = new HttpPost(url);// 創建HttpPost對象
- try {
- String param="__VIEWSTATE=D&__EVENTTARGET=AspNetPager1&__EVENTARGUMENT=5&AspNetPager1_input=2";
- StringEntity entity = new StringEntity(param);
- entity.setContentType("application/x-www-form-urlencoded");
- entity.setContentEncoding("gb2312");
- httpRequst.setEntity(entity);
- HttpResponse httpResponse = new DefaultHttpClient()
- .execute(httpRequst);
- System.out.println(httpResponse.getStatusLine().getStatusCode());
- if (httpResponse.getStatusLine().getStatusCode() == 200) {
- HttpEntity httpEntity = httpResponse.getEntity();
- result = EntityUtils.toString(httpEntity);// 取出應答字符串
- System.out.println(result);
- }
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- result = e.getMessage().toString();
- }
以發送連接請求時,需要設置鏈接超時和請求超時等參數,否則會長期停止或者崩潰。
- HttpParams httpParameters = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParameters, 10*1000);//設置請求超時10秒
- HttpConnectionParams.setSoTimeout(httpParameters, 10*1000); //設置等待數據超時10秒
- HttpConnectionParams.setSocketBufferSize(params, 8192);
- HttpClient httpclient = new DefaultHttpClient(httpParameters); //此時構造DefaultHttpClient時將參數傳入
原文地址:http://blog.csdn.net/zzq900503/article/details/17320807