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来访问,一般用于需要提交表单的情形。

Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页的更多相关文章

  1. python3.5+django2.0快速入门(二)

    昨天写了python3.5+django2.0快速入门(一)今天将讲解配置数据库,创建模型,还有admin的后台管理. 配置数据库 我们打开mysite/mysite/settings.py这个文件. ...

  2. ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载

    ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...

  3. python3.5+django2.0快速入门(一)

    因为这篇教程需要用到anaconda的一些操作,如果还不懂anaconda的操作的同学可以看下这篇文章python 入门学习之anaconda篇. 创建python3+的开发环境 直接在终端输入:co ...

  4. TensorFlow 2.0 快速入门指南 | iBooker·ApacheCN

    原文:TensorFlow 2.0 Quick Start Guide 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生活 ...

  5. JAVA中使用Apache HttpComponents Client的进行GET/POST请求使用案例

    一.简述需求 平时我们需要在JAVA中进行GET.POST.PUT.DELETE等请求时,使用第三方jar包会比较简单.常用的工具包有: 1.https://github.com/kevinsawic ...

  6. Thinkphp5.0快速入门笔记(3)

    学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 快速入门第三节 获取当前的请求 ...

  7. Thinkphp5.0快速入门笔记(2)

    学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 示例建立新的模块和控制器 在a ...

  8. rancher2.0快速入门

    注意:本入门指南的目的是让您快速的运行一个Rancher2.0环境,它不适用于生产.有关更全面的说明,请查阅Rancher安装. 本教程将指导您完成: 安装Rancher v2.0 : 创建第一个集群 ...

  9. Thinkphp5.0快速入门笔记(1)

    学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. Composer安装和更新 C ...

随机推荐

  1. Java类与类之间关系总结

    继承,依赖,关联,聚合,组合 一般来说依赖和关联是类似的,关联是强依赖,聚合和组合是一类,组合属于强聚合. 继承:一般是子类和父类之间的关系,关键字extends 依赖:可以这样记忆,做某件事必须要依 ...

  2. 2015-09-28 Javascript

    1.Javascript是什么? JavaScript是一种脚本语言,结构简单,使用方便,其代码可以直接放入HTML文档中,可以直接在支持JavaScript的浏览器中运行.JavaSript. Ja ...

  3. ajax提交请求为啥url要用这个函数encodeURI

    参考如下: 如果你是通过form提交的,那就不需要用这个了.但是如果是你使用url的方式例如:ajax提交到后台的,就需要对url进行encodeURI编码,否则,会导致后台出现各种乱码,不加enco ...

  4. window.location.href 和self.location的区别

    你从字面上就可以理解到 window 指的是当前窗口 而 self 指的是自己 在HTML 中 由于页面可以镶嵌页面 所以这2个就有了 区别 比如说 我有个页面A.HTML 里面嵌套了一个B.HTML ...

  5. [php基础]记录PHP错误日志 display_errors与log_errors的区别

    display_errors 错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴露出非常多的敏感信息,为攻击者下一步攻击提供便利.推荐关闭此选项. display ...

  6. Android应用清单文件:AndroidManifest.xml

    AndroidMainfest.xml清单文件是每个Android项目所必需的,它是整个Android应用的全家描述文件. <?xml version="1.0" encod ...

  7. mysql的perror

    eg: 执行:perror 1064 返回:MySQL error code 1064 (ER_PARSE_ERROR): %s near '%-.80s' at line %d           ...

  8. JavaScript 函数作用域和闭包

    函数作用域和闭包  词法作用域   它们在定义它们的作用域里运行,而不是在执行的作用域运行,但是只有在运行时,作用域链中的属性才被 定义(调用对象),此时,可访问任何当前的绑定.   调用对象     ...

  9. 武汉科技大学ACM :1004: A+B for Input-Output Practice (IV)

    Problem Description Your task is to Calculate the sum of some integers. Input Input contains multipl ...

  10. Android开发之InstanceState详解(转)

    本文来自:http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html 本文介绍Android中关于Activity的两个神秘方法: ...