HttpClient基础教程
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对象。
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpGet httpGet = new HttpGet("http://www.baidu.com/");
- CloseableHttpResponse response = httpClient.execute(httpGet);
注意,必需要加上http://的前缀,否则会报:Target host is null异常。
(2)获取Response对象的Entity。
- HttpEntity entity = response.getEntity();
注:HttpClient将Response的正文及Request的POST/PUT方法中的正文均封装成一个HttpEntity对象。可以通过entity.getContenType(),entity.getContentLength()等方法获取到正文的相关信息。但最重要的方法是通过getContent()获取到InputStream对象。
(3)通过Entity获取到InputStream对象,然后对返回内容进行处理。
- is = entity.getContent();
- sc = new Scanner(is);
- // String filename = path.substring(path.lastIndexOf('/')+1);
- String filename = "2.txt";
- os = new PrintWriter(filename);
- while (sc.hasNext()) {
- os.write(sc.nextLine());
- }
使用HtppClient下载一个网页的完整代码如下:
- package com.ljh.test;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.io.Writer;
- import java.util.Scanner;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- public class DownloadWebPage{
- public static void downloadPagebyGetMethod() throws IOException {
- // 1、通过HttpGet获取到response对象
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpGet httpGet = new HttpGet("http://www.baidu.com/");
- CloseableHttpResponse response = httpClient.execute(httpGet);
- InputStream is = null;
- Scanner sc = null;
- Writer os = null;
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- try {
- // 2、获取response的entity。
- HttpEntity entity = response.getEntity();
- // 3、获取到InputStream对象,并对内容进行处理
- is = entity.getContent();
- sc = new Scanner(is);
- // String filename = path.substring(path.lastIndexOf('/')+1);
- String filename = "2.txt";
- os = new PrintWriter(filename);
- while (sc.hasNext()) {
- os.write(sc.nextLine());
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } finally {
- if (sc != null) {
- sc.close();
- }
- if (is != null) {
- is.close();
- }
- if (os != null) {
- os.close();
- }
- if (response != null) {
- response.close();
- }
- }
- }
- }
- public static void main(String[] args) {
- try {
- downloadPagebyGetMethod();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
注意:直接将HttpGet改为HttpPost,返回的结果有误,百度返回302状态,即重定向,新浪返回拒绝访问。怀疑大多网站均不允许POST方法直接访问网站。
HttpClient基础教程的更多相关文章
- 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/ ...
- matlab基础教程——根据Andrew Ng的machine learning整理
matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...
- <<Bootstrap基础教程>> 新书出手,有心栽花花不开,无心插柳柳成荫
并非闲的蛋疼,做技术也经常喜欢蛋疼,纠结于各种技术,各种需求变更,还有一个很苦恼的就是UI总是那么不尽人意.前不久自己开源了自己做了多年的仓储项目(开源地址:https://github.com/he ...
- Memcache教程 Memcache零基础教程
Memcache是什么 Memcache是danga.com的一个项目,来分担数据库的压力. 它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建立一个Hash ...
- Selenium IDE 基础教程
Selenium IDE 基础教程 1.下载安装 a 在火狐浏览其中搜索附件组件,查找 Selenium IDE b 下载安装,然后重启firefox 2.界面讲解 在菜单- ...
- html快速入门(基础教程+资源推荐)
1.html究竟是什么? 从字面上理解,html是超文本标记语言hyper text mark-up language的首字母缩写,指的是一种通用web页面描述语言,是用来描述我们打开浏览器就能看到的 ...
- 转发-UI基础教程 – 原生App切图的那些事儿
UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切 ...
- 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...
- oracle基础教程(8)oracle修改字符集
oracle基础教程(8)oracle修改字符集 1.用dba连接数据库 -->sqlplus / as sysdba 2.查看字符集 -->SELECT parameter, value ...
随机推荐
- [android]android开发中的运行错误之:adb.exe
调试的时候出现一下错误: The connection to adb is down, and a servera error has occured.You must restart adb and ...
- After a rest, go on
busy during the whole May holiday. running between S and H, waste much time leaving things behind. t ...
- vim常用指令一览
插入指令 i 在光标处插入字符 I 在光标所在行最前插入 a 在光标所在字符后面插入 A 在光标所在行的最后插入 o 在光表所在行下面插入一行 O 在光标所在行上面插入一行 r 替换光标所在字符,只替 ...
- 【solr基础教程之九】客户端
一.Java Script 1.由于Solr本身可以返回Json格式的结果,而JavaScript对于处理Json数据具有天然的优势,因此使用JavaScript实现Solr客户端是一个很好的选择. ...
- 快速下单!简化EcStore的购物结算流程
EcStore拥有完善的购物车功能,方便顾客浏览挑选商品,但是在提交订单时必须要求用户先登录注册 如果是未注册用户还多出一个注册用户的步骤这些多出来的步骤和操作会影响购物下单的流畅性,降低了用户购物下 ...
- php 数组Array 删除指定键名值
if(array_key_exists('keyname',$array)){ //检查数组中此键名是否存在: unset($array['keyname']); //删除后位置仍然保留,但清空了键名 ...
- Eclipse工程乱码解决
eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工程时,如果该工程的编码方式与eclipse中设置的编码方式不同 ...
- 怎样使用淘宝npm镜像
淘宝的 NPM 镜像是一个完整的npmjs.org镜像.你可以用此代替官方版本(只读),同步频率目前为 15分钟 一次以保证尽量与官方服务同步. 当前 registry.npm.taobao.org ...
- 大端模式&小端模式、主机序&网络序、入栈地址高低问题
一.大端模式&小端模式 所谓的“大端模式”,是指数据的低位(就是权值较小的后面那几位)保存在内存的高地址中,而数据的高位,保存在内存的低地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处 ...
- org.springframework.dao.EmptyResultDataAccessException
public Wcrash getWcrashInfo(int id) { String sql = "select plateform_id,android_version,app_ver ...
