HttpClient 发送Post,Get请求例子(包含设置请求头信息和获取返回头信息)

Java代码   收藏代码
  1. package com.test.action;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  
  8. import org.apache.commons.httpclient.Header;  
  9. import org.apache.commons.httpclient.HttpClient;  
  10. import org.apache.commons.httpclient.HttpException;  
  11. import org.apache.commons.httpclient.HttpStatus;  
  12. import org.apache.commons.httpclient.methods.PostMethod;  
  13. import org.apache.commons.httpclient.params.HttpMethodParams;  
  14.   
  15. /** 
  16.  *  
  17.  * 依赖 commons-httpclient-3.1.jar  commons-codec-1.4.jar  
  18.  * @author tianjun 
  19.  *  
  20.  */  
  21. public class PostTest  
  22. {  
  23.     public static void main(String[] args)  
  24.     {  
  25.         // (1)构造HttpClient的实例  
  26.         HttpClient httpClient = new HttpClient();  
  27.   
  28.         // (2)创建POST方法的实例  
  29.         PostMethod postMethod = new PostMethod(  
  30.                 "http://localhost:8080/b/test1.do");  
  31.         // GetMethod getMethod = new  
  32.         // GetMethod("http://localhost:8080/b/test1.do");  
  33.   
  34.         // (3)设置http request头  
  35.         List<Header> headers = new ArrayList<Header>();  
  36.         headers.add(new Header("tianjun_key""tianjun_value"));  
  37.         httpClient.getHostConfiguration().getParams().setParameter(  
  38.                 "http.default-headers", headers);  
  39.   
  40.         // 使用系统提供的默认的恢复策略  
  41.         postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
  42.                 new DefaultHttpMethodRetryHandler());  
  43.         // getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  
  44.         // new DefaultHttpMethodRetryHandler());  
  45.   
  46.         try  
  47.         {  
  48.             // (4)执行postMethod  
  49.             int statusCode = httpClient.executeMethod(postMethod);  
  50.             if (statusCode != HttpStatus.SC_OK)  
  51.             {  
  52.                 System.err.println("Method failed: "  
  53.                         + postMethod.getStatusLine());  
  54.             }  
  55.   
  56.             // (5)读取response头信息  
  57.             Header headerResponse = postMethod  
  58.                     .getResponseHeader("response_key");  
  59.             String headerStr = headerResponse.getValue();  
  60.             // (6)读取内容  
  61.             byte[] responseBody = postMethod.getResponseBody();  
  62.             // (7) 处理内容  
  63.             System.out.println(headerStr);  
  64.             System.out.println(new String(responseBody));  
  65.         } catch (HttpException e)  
  66.         {  
  67.             // 发生致命的异常,可能是协议不对或者返回的内容有问题  
  68.             System.out.println("Please check your provided http address!");  
  69.             e.printStackTrace();  
  70.         } catch (IOException e)  
  71.         {  
  72.             e.printStackTrace();  
  73.         } catch (Exception e)  
  74.         {  
  75.             e.printStackTrace();  
  76.         } finally  
  77.         {  
  78.             // 释放连接  
  79.             postMethod.releaseConnection();  
  80.         }  
  81.     }  
  82. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值