使用HttpClient发送GET请求和带有表单参数的POST请求

http://blog.csdn.net/jadyer/article/details/7615830

 

 

  1. package  com.jadyer.util;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.io.UnsupportedEncodingException;  
  5. import  java.util.ArrayList;  
  6. import  java.util.List;  
  7.   
  8. import  org.apache.http.HttpEntity;  
  9. import  org.apache.http.HttpResponse;  
  10. import  org.apache.http.NameValuePair;  
  11. import  org.apache.http.ParseException;  
  12. import  org.apache.http.client.ClientProtocolException;  
  13. import  org.apache.http.client.HttpClient;  
  14. import  org.apache.http.client.entity.UrlEncodedFormEntity;  
  15. import  org.apache.http.client.methods.HttpGet;  
  16. import  org.apache.http.client.methods.HttpPost;  
  17. import  org.apache.http.impl.client.DefaultHttpClient;  
  18. import  org.apache.http.message.BasicNameValuePair;  
  19. import  org.apache.http.util.EntityUtils;  
  20.   
  21. /**  
  22.  * 使用HttpClient模拟HTTP访问  
  23.  * @see ===============================================================================================================================  
  24.  * @see HttpClient代表了一个HTTP客户端,HttpClient接口中定义了大多数基本的HTTP请求执行行为  
  25.  * @see HttpEntity是发送或接收消息的载体,它可以通过HttpResponse.getEntity()获取到  
  26.  * @see HttpConnection代表了一个HTTP连接  
  27.  * @see ===============================================================================================================================  
  28.  * @author http://blog.csdn.net/jadyer  
  29.  * @editor Feb 1, 2012 3:02:27 PM  
  30.  */   
  31. public   class  HttpClientUtil {  
  32.     public   static   void  main(String[] args) throws  Exception{  
  33.         //String requestUrl = "http://123.125.97.xxx:8804/ecpaycus/test/JFB_web_visit.jsp?phoneNo=18600000001";   
  34.         //System.out.println(sendGetRequest(requestUrl));   
  35.         String requestUrl = "http://127.0.0.1:8088/test/web/userac" ;  
  36.         System.out.println(sendPostRequest(requestUrl));  
  37.     }  
  38.       
  39.     /**  
  40.      * 发送GET请求  
  41.      * @param requestUrl 请求的地址(含参数)  
  42.      * @return 响应内容  
  43.      */   
  44.     @SuppressWarnings ( "finally" )  
  45.     public   static  String sendGetRequest(String requestUrl) {  
  46.         long  responseLength =  0 ;        //响应长度   
  47.         String responseContent = null//响应内容   
  48.         HttpClient httpClient = new  DefaultHttpClient();  //创建默认的httpClient实例   
  49.         HttpGet httpGet = new  HttpGet(requestUrl);        //创建HttpGet   
  50.         try  {  
  51.             HttpResponse response = httpClient.execute(httpGet); //执行GET请求   
  52.             HttpEntity entity = response.getEntity();            //获取响应实体   
  53.             if  ( null  != entity) {  
  54.                 responseLength = entity.getContentLength();  
  55.                 responseContent = EntityUtils.toString(entity, "UTF-8" );  
  56.                 EntityUtils.consume(entity); //Consume response content   
  57.             }  
  58.             System.out.println("请求地址: "  + httpGet.getURI());  
  59.             System.out.println("响应状态: "  + response.getStatusLine());  
  60.             System.out.println("响应长度: "  + responseLength);  
  61.             System.out.println("响应内容: "  + responseContent);  
  62.         } catch  (ClientProtocolException e) {  
  63.             //该异常通常是协议错误导致   
  64.             //比如构造HttpGet对象时传入的协议不对(将"http"写成"htp")或者服务器端返回的内容不符合HTTP协议要求等   
  65.             e.printStackTrace();  
  66.         } catch  (ParseException e) {  
  67.             e.printStackTrace();  
  68.         } catch  (IOException e) {  
  69.             //该异常通常是网络原因引起的,如HTTP服务器未启动等   
  70.             e.printStackTrace();  
  71.         } finally  {  
  72.             httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源   
  73.             return  responseContent;  
  74.         }  
  75.     }  
  76.       
  77.     /**  
  78.      * 发送带有表单参数的POST请求  
  79.      * @param requestUrl 请求的地址(不含参数)  
  80.      * @return 响应内容  
  81.      */   
  82.     @SuppressWarnings ( "finally" )  
  83.     public   static  String sendPostRequest(String requestUrl) {  
  84.         long  responseLength =  0 ;        //响应长度   
  85.         String responseContent = null//响应内容   
  86.         HttpClient httpClient = new  DefaultHttpClient();                  //创建默认的httpClient实例   
  87.         HttpPost httpPost = new  HttpPost(requestUrl);                     //创建HttpPost   
  88.         List<NameValuePair> formParams = new  ArrayList<NameValuePair>();  //创建参数队列   
  89.         formParams.add(new  BasicNameValuePair( "username""jadyer" ));  
  90.         formParams.add(new  BasicNameValuePair( "password""hongyu" ));  
  91.         try  {  
  92.             httpPost.setEntity(new  UrlEncodedFormEntity(formParams,  "UTF-8" ));  
  93.             HttpResponse response = httpClient.execute(httpPost); //执行POST请求   
  94.             HttpEntity entity = response.getEntity();             //获取响应实体   
  95.             if  ( null  != entity) {  
  96.                 responseLength = entity.getContentLength();  
  97.                 responseContent = EntityUtils.toString(entity, "UTF-8" );  
  98.                 EntityUtils.consume(entity); //Consume response content   
  99.             }  
  100.             System.out.println("请求地址: "  + httpPost.getURI());  
  101.             System.out.println("响应状态: "  + response.getStatusLine());  
  102.             System.out.println("响应长度: "  + responseLength);  
  103.             System.out.println("响应内容: "  + responseContent);  
  104.         } catch  (UnsupportedEncodingException e) {  
  105.             e.printStackTrace();  
  106.         } catch  (ClientProtocolException e) {  
  107.             e.printStackTrace();  
  108.         } catch  (ParseException e) {  
  109.             e.printStackTrace();  
  110.         } catch  (IOException e) {  
  111.             e.printStackTrace();  
  112.         } finally  {  
  113.             httpClient.getConnectionManager().shutdown(); //关闭连接,释放资源   
  114.             return  responseContent;  
  115.         }  
  116.     }  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值