1、HttpClient相关的重要资料

官方网站:http://hc.apache.org/

API:http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/index.html

tutorial: http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/index.html  【PDF版本】http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/pdf/httpclient-tutorial.pdf

2、HttpClient有2个版本

org.apache.http.impl.client.HttpClients 与 org.apache.commons.httpclient.HttpClient

目前后者已被废弃,apache已不再支持。

一般而言,使用HttpClient均需导入httpclient.jar与httpclient-core.jar2个包。

3、使用HttpClient进行网络处理的基本步骤

(1)通过get的方式获取到Response对象。

[java] view
plain
copy

  1. CloseableHttpClient httpClient = HttpClients.createDefault();
  2. HttpGet httpGet = new HttpGet("http://www.baidu.com/");
  3. CloseableHttpResponse response = httpClient.execute(httpGet);

注意,必需要加上http://的前缀,否则会报:Target host is null异常。

(2)获取Response对象的Entity。

[java] view
plain
copy

  1. HttpEntity entity = response.getEntity();

注:HttpClient将Response的正文及Request的POST/PUT方法中的正文均封装成一个HttpEntity对象。可以通过entity.getContenType(),entity.getContentLength()等方法获取到正文的相关信息。但最重要的方法是通过getContent()获取到InputStream对象。

(3)通过Entity获取到InputStream对象,然后对返回内容进行处理。

[java] view
plain
copy

  1. is = entity.getContent();
  2. sc = new Scanner(is);
  3. // String filename = path.substring(path.lastIndexOf('/')+1);
  4. String filename = "2.txt";
  5. os = new PrintWriter(filename);
  6. while (sc.hasNext()) {
  7. os.write(sc.nextLine());
  8. }

使用HtppClient下载一个网页的完整代码如下:

[java] view
plain
copy

  1. package com.ljh.test;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PrintWriter;
  5. import java.io.Writer;
  6. import java.util.Scanner;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpStatus;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClients;
  14. public class DownloadWebPage{
  15. public static void downloadPagebyGetMethod() throws IOException {
  16. // 1、通过HttpGet获取到response对象
  17. CloseableHttpClient httpClient = HttpClients.createDefault();
  18. HttpGet httpGet = new HttpGet("http://www.baidu.com/");
  19. CloseableHttpResponse response = httpClient.execute(httpGet);
  20. InputStream is = null;
  21. Scanner sc = null;
  22. Writer os = null;
  23. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  24. try {
  25. // 2、获取response的entity。
  26. HttpEntity entity = response.getEntity();
  27. // 3、获取到InputStream对象,并对内容进行处理
  28. is = entity.getContent();
  29. sc = new Scanner(is);
  30. // String filename = path.substring(path.lastIndexOf('/')+1);
  31. String filename = "2.txt";
  32. os = new PrintWriter(filename);
  33. while (sc.hasNext()) {
  34. os.write(sc.nextLine());
  35. }
  36. } catch (ClientProtocolException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (sc != null) {
  40. sc.close();
  41. }
  42. if (is != null) {
  43. is.close();
  44. }
  45. if (os != null) {
  46. os.close();
  47. }
  48. if (response != null) {
  49. response.close();
  50. }
  51. }
  52. }
  53. }
  54. public static void main(String[] args) {
  55. try {
  56. downloadPagebyGetMethod();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }

注意:直接将HttpGet改为HttpPost,返回的结果有误,百度返回302状态,即重定向,新浪返回拒绝访问。怀疑大多网站均不允许POST方法直接访问网站。

HttpClient基础教程的更多相关文章

  1. HttpClient基础教程 分类: C_OHTERS 2014-05-18 23:23 2600人阅读 评论(0) 收藏

    1.HttpClient相关的重要资料 官方网站:http://hc.apache.org/ API:http://hc.apache.org/httpcomponents-client-4.3.x/ ...

  2. matlab基础教程——根据Andrew Ng的machine learning整理

    matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...

  3. <<Bootstrap基础教程>> 新书出手,有心栽花花不开,无心插柳柳成荫

    并非闲的蛋疼,做技术也经常喜欢蛋疼,纠结于各种技术,各种需求变更,还有一个很苦恼的就是UI总是那么不尽人意.前不久自己开源了自己做了多年的仓储项目(开源地址:https://github.com/he ...

  4. Memcache教程 Memcache零基础教程

    Memcache是什么 Memcache是danga.com的一个项目,来分担数据库的压力. 它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建立一个Hash ...

  5. Selenium IDE 基础教程

    Selenium IDE 基础教程 1.下载安装     a 在火狐浏览其中搜索附件组件,查找 Selenium IDE     b 下载安装,然后重启firefox 2.界面讲解      在菜单- ...

  6. html快速入门(基础教程+资源推荐)

    1.html究竟是什么? 从字面上理解,html是超文本标记语言hyper text mark-up language的首字母缩写,指的是一种通用web页面描述语言,是用来描述我们打开浏览器就能看到的 ...

  7. 转发-UI基础教程 – 原生App切图的那些事儿

    UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切 ...

  8. 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...

  9. oracle基础教程(8)oracle修改字符集

    oracle基础教程(8)oracle修改字符集 1.用dba连接数据库 -->sqlplus / as sysdba 2.查看字符集 -->SELECT parameter, value ...

随机推荐

  1. hdu2037 经典贪心入门

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. uva11630 or hdu2987 Cyclic antimonotonic permutations(构造水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Cyclic antimonotonic permutations Time Li ...

  3. popup

    http://vast-engineering.github.com/jquery-popup-overlay/ http://photoswipe.com/

  4. python selenium初入

    ubuntu python3.4 1.安装selenium, pip 安装 pip install selenium 2.selenium版本2.53.x 试过从官网下载的selenium3,但是fi ...

  5. Flask的session——关于写扩展所学习到的

    这两天端午节.趁着端午节没事干,写了个flask的扩展--flask-RedisSession 在flask中使用该扩展可以让你借助redis数据库轻松获得server-side session. 这 ...

  6. Android的AndroidManifest.xml文件的详解

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...

  7. cxf2.7.10 与 spring3.0.5集成

    开发环境: NetBeans7.4 Tomcat 6.0.32 一 服务端: 1:新建JavaWeb工程 cxfspring-server,导入jar包如下图所示: 2:在web.xml文件中添加如下 ...

  8. WIN7_64位系统安装oracle以及PLSQL方法(不用装32位oracle客户端)

    一.oracle10g安装,比较简单 1.去Oracle网站下载Vista版的Oracle:Oracle Database 10g Release 2 (10.2.0.4) for Microsoft ...

  9. cf479D Long Jumps

    D. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>

    B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 2048/1024KB (Java/Others) Submit ...