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方法直接访问网站。

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. [LeetCode] Random Pick Index 随机拾取序列
  2. redis学习笔记
  3. 2016暑假多校联合---Windows 10
  4. python—基础类的那点儿所以然
  5. 上传文件时$_FILES为空的解决方法
  6. 为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?
  7. postgresql 连接数
  8. 51nod1201 整数划分
  9. Informatica9.6.1在Linux Red Hat 5.8上安装遇到的有关问题整理_4
  10. ComboBox控件
  11. Objective-C 【NSString-字符串比较&前后缀检查及搜索】
  12. 空值排序(oracle和sqlserver)
  13. QRMaker生成二维码,支持中文
  14. 【HighCharts系列教程】二、Highcharts结构及API文档
  15. ant使用
  16. Dynamic learning rate in training - 培训中的动态学习率
  17. mysql安装完成之后为root用户添加密码
  18. arcpy加载mxd文件时,无效的MXD路径,提示assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
  19. luogu1984 烧水问题 (找规律)
  20. [原][osg][osgearth]倾斜摄影1.介绍

热门文章

  1. linux中内存泄漏的检測(五)记录内存泄漏的代码
  2. windows 2016 配置 VNC 服务
  3. js刷新页面有哪几种方法
  4. HTTP协议建立连接、通讯与关闭连接全过程
  5. 从“窃听门”事件解读手机Rootkit攻击
  6. Vue 打包后报错 Uncaught TypeError: Cannot redefine property: $router
  7. Js 栈和堆的实现
  8. 三、Docker镜像的相关操作
  9. Shiro学习总结(2)——Apache Shiro快速入门教程
  10. 00096_Properties类