HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页

36 篇文章 0 订阅

Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 httpcilent4.0无法做到代码向后兼容,升级比较麻烦。我在做项目之余找时间研究了一下,写了一套3.1与4.0对比的代码,不求面面俱到,但 求简单易懂。如果代码用到真实项目中,还需要考虑诸如代理、Header、异常处理之类的问题。

Http POST方法得到www.g.cn的源码:

 

  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import org.apache.commons.httpclient.NameValuePair;  
  5. import org.apache.commons.httpclient.methods.PostMethod;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.ParseException;  
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  10. import org.apache.http.client.methods.HttpPost;  
  11. import org.apache.http.impl.client.DefaultHttpClient;  
  12. import org.apache.http.message.BasicNameValuePair;  
  13. import org.apache.http.protocol.HTTP;  
  14. import org.apache.http.util.EntityUtils;  
  15. public class PostSample {  
  16.     public static void main(String[] args) throws ParseException, IOException {  
  17.         String url = "http://www.g.cn/";  
  18.         System.out.println(url);  
  19.         System.out.println("Visit google using Apache commons-httpclient 3.1:");  
  20.         List<NameValuePair> data3 = new ArrayList<NameValuePair>();  
  21.         data3.add(new NameValuePair("username""testuser"));  
  22.         data3.add(new NameValuePair("password""testpassword"));  
  23.         System.out.println(post3(url, data3));  
  24.         System.out.println("Visit google using Apache HttpComponents Client 4.0:");  
  25.         List<BasicNameValuePair> data4 = new ArrayList<BasicNameValuePair>();  
  26.         data4.add(new BasicNameValuePair("username""testuser"));  
  27.         data4.add(new BasicNameValuePair("password""testpassword"));  
  28.         System.out.println(post4(url, data4));  
  29.     }  
  30.     /** 使用Apache commons-httpclient 3.1,POST方法访问网页 */  
  31.     public static String post3(String url, List<NameValuePair> data) throws IOException {  
  32.         org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();  
  33.         PostMethod postMethod = new PostMethod(url);  
  34.         postMethod.setRequestBody(data.toArray(new NameValuePair[data.size()]));  
  35.         try {  
  36.             System.out.println("<< Response: " + httpClient.executeMethod(postMethod));  
  37.             return postMethod.getResponseBodyAsString();  
  38.         } finally {  
  39.             postMethod.releaseConnection();  
  40.         }  
  41.     }  
  42.     /** 使用Apache HttpComponents Client 4.0,POST方法访问网页 */  
  43.     private static String post4(String url, List<? extends org.apache.http.NameValuePair> data)  
  44.             throws ParseException, IOException {  
  45.         org.apache.http.client.HttpClient client = new DefaultHttpClient();  
  46.         HttpPost httpost = new HttpPost(url);  
  47.         httpost.setEntity(new UrlEncodedFormEntity(data, HTTP.UTF_8));  
  48.         try {  
  49.             HttpResponse response = client.execute(httpost);  
  50.             HttpEntity entity = response.getEntity();  
  51.             System.out.println("<< Response: " + response.getStatusLine());  
  52.             if (entity != null) {  
  53.                 return EntityUtils.toString(entity);  
  54.             }  
  55.             return null;  
  56.         } finally {  
  57.             client.getConnectionManager().shutdown();  
  58.         }  
  59.     }  
  60. }  

 

当然www.g.cn不必要通过post来访问,一般用于需要提交表单的情形。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值