httpClient----Post 和 GET请求

在http请求失败是要进行下列操作:

[java]  view plain copy
  1. if (response.getStatusLine ().getStatusCode () != 200) {    
  2.        get.abort();   
  3.        return null;   
  4. }   

HttpGet操作:

[java]  view plain copy
  1. public final static void main(String[] args) throws Exception {  
  2.     HttpClient httpclient = new DefaultHttpClient();  
  3.     try {  
  4.         HttpGet httpget = new HttpGet("http://www.apache.org/");  
  5.         System.out.println("executing request " + httpget.getURI());  
  6.         HttpResponse response = httpclient.execute(httpget);  
  7.         HttpEntity entity = response.getEntity();  
  8.   
  9.         System.out.println("----------------------------------------");  
  10.         System.out.println(response.getStatusLine());  
  11.         if (entity != null) {  
  12.             System.out.println("Response content length: " + entity.getContentLength());  
  13.         }  
  14.         System.out.println("----------------------------------------");  
  15.   
  16.         InputStream inSm = entity.getContent();  
  17.         Scanner inScn = new Scanner(inSm);  
  18.         while (inScn.hasNextLine()) {  
  19.             System.out.println(inScn.nextLine());  
  20.         }  
  21.         // Do not feel like reading the response body  
  22.         // Call abort on the request object  
  23.         httpget.abort();  
  24.     } finally {  
  25.         // When HttpClient instance is no longer needed,  
  26.         // shut down the connection manager to ensure  
  27.         // immediate deallocation of all system resources  
  28.         httpclient.getConnectionManager().shutdown();  
  29.     }  
  30. }  

读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的:

[java]  view plain copy
  1. public class Demo1 {  
  2.   
  3.     /** 
  4.      * 用 get 方法访问 www.apache.org 并返回内容 
  5.      * 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         //创建默认的 HttpClient 实例  
  10.         HttpClient httpClient = new DefaultHttpClient();  
  11.         try {  
  12.             //创建 httpUriRequest 实例  
  13.             HttpGet httpGet = new HttpGet("http://www.apache.org/");  
  14.             System.out.println("uri=" + httpGet.getURI());  
  15.   
  16.             //执行 get 请求  
  17.             HttpResponse httpResponse = httpClient.execute(httpGet);  
  18.   
  19.             //获取响应实体  
  20.             HttpEntity httpEntity = httpResponse.getEntity();  
  21.             //打印响应状态  
  22.             System.out.println(httpResponse.getStatusLine());  
  23.             if (httpEntity != null) {  
  24.                 //响应内容的长度  
  25.                 long length = httpEntity.getContentLength();  
  26.                 //响应内容  
  27.                 String content = EntityUtils.toString(httpEntity);  
  28.   
  29.                 System.out.println("Response content length:" + length);  
  30.                 System.out.println("Response content:" + content);  
  31.             }  
  32.   
  33.             //有些教程里没有下面这行  
  34.             httpGet.abort();  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         } finally {  
  38.             //关闭连接,释放资源  
  39.             httpClient.getConnectionManager().shutdown();  
  40.         }  
  41.     }  
  42. }  

分析请求包中这六个头信息: httpget.setHeader("Accept-Encoding""gzip, deflate")可能产生乱码

[java]  view plain copy
  1. /** 
  2.  * @param args 
  3.  * @throws IOException 
  4.  */  
  5. public static void main(String[] args) {  
  6.     HttpClient httpClient = new DefaultHttpClient();  
  7.     try {  
  8.         HttpGet httpget = new HttpGet("http://www.iteye.com");  
  9.   
  10.         httpget.setHeader("Accept""text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  11.         httpget.setHeader("Accept-Language""zh-cn,zh;q=0.5");  
  12.         httpget.setHeader("User-Agent""Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");  
  13.         httpget.setHeader("Accept-Encoding""gzip, deflate");  
  14.         httpget.setHeader("Accept-Charset""GB2312,utf-8;q=0.7,*;q=0.7");  
  15.         httpget.setHeader("Host""www.iteye.com");  
  16.         httpget.setHeader("Connection""Keep-Alive");  
  17.   
  18.         HttpResponse response = httpClient.execute(httpget);  
  19.         HttpEntity entity = response.getEntity();  
  20.   
  21.         System.out.println("----------------------------------------");  
  22.         System.out.println(response.getStatusLine());  
  23.         if (entity != null) {  
  24.             System.out.println("Response content length: " + entity.getContentLength());  
  25.         }  
  26.         System.out.println("----------------------------------------");  
  27.   
  28.         InputStream inSm = entity.getContent();  
  29.         Scanner inScn = new Scanner(inSm);  
  30.         while (inScn.hasNextLine()) {  
  31.             System.out.println(inScn.nextLine());  
  32.         }  
  33.         // Do not feel like reading the response body  
  34.         // Call abort on the request object  
  35.         httpget.abort();  
  36.     } catch (Exception e) {  
  37.         e.printStackTrace();  
  38.     } finally {  
  39.         // When HttpClient instance is no longer needed,  
  40.         // shut down the connection manager to ensure  
  41.         // immediate deallocation of all system resources  
  42.         httpClient.getConnectionManager().shutdown();  
  43.     }  
  44. }  

HttpPost操作:

[java]  view plain copy
  1. public class Demo2 {  
  2.   
  3.     /** 
  4.      * 用post方法访问本地应用根据传递参数不同,返回不同结果 
  5.      * 
  6.      * @param args 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         //创建默认的 HttpClient 实例  
  10.         HttpClient httpClient = new DefaultHttpClient();  
  11.   
  12.         HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet");  
  13.   
  14.         List<NameValuePair> formParams = new ArrayList<NameValuePair>();  
  15.         formParams.add(new BasicNameValuePair("param1""***"));  
  16.         UrlEncodedFormEntity urlEncodedFormEntity;  
  17.   
  18.         try {  
  19.             urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");  
  20.             httpPost.setEntity(urlEncodedFormEntity);  
  21.             System.out.println("execurting request:" + httpPost.getURI());  
  22.             HttpResponse httpResponse = null;  
  23.             httpResponse = httpClient.execute(httpPost);  
  24.             HttpEntity httpEntity = httpResponse.getEntity();  
  25.             if (httpEntity != null) {  
  26.                 String content = EntityUtils.toString(httpEntity, "UTF-8");  
  27.                 System.out.println("Response content:" + content);  
  28.             }  
  29.         } catch (ClientProtocolException e) {  
  30.             e.printStackTrace();  
  31.         } catch (UnsupportedEncodingException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         } finally {  
  36.             //关闭连接,释放资源  
  37.             httpClient.getConnectionManager().shutdown();  
  38.         }  
  39.     }  
  40. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值