HttpClient4基础1--通过匿名代理访问网页
来源:互联网 发布:c语言变量 编辑:程序博客网 时间:2024/05/20 15:12
http://llying.iteye.com/blog/456455
HttpClient发布4.0了 而且底层完全重写了,据说无论是效率还是结构都有质的飞跃。
现在也要与时具进,研究研究。
- package test.httpclient4.proxy;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpHost;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.StatusLine;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.conn.params.ConnRoutePNames;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicStatusLine;
- import org.apache.http.util.EntityUtils;
- public class GetHttpByProxy {
- public static void main(String[] args) throws ClientProtocolException,
- IOException {
- //实例化一个HttpClient
- HttpClient httpClient = new DefaultHttpClient();
- //设定目标站点
- HttpHost httpHost = new HttpHost("www.shanhe114.com");
- //设置代理对象 ip/代理名称,端口
- HttpHost proxy = new HttpHost("192.168.1.28", 5608);
- //对HttpClient对象设置代理
- httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
- proxy);
- HttpGet httpGet = new HttpGet("/");
- //这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾
- //HttpGet httpGet = new HttpGet("http://www.shanhe114.com/");
- //HttpResponse response = httpClient.execute(httpGet);
- HttpResponse response = httpClient.execute(httpHost, httpGet);
- if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
- //请求成功
- //取得请求内容
- HttpEntity entity = response.getEntity();
- //显示内容
- if (entity != null) {
- // 显示结果
- BufferedReader reader = new BufferedReader(new InputStreamReader(entity
- .getContent(), "UTF-8"));
- String line = null;
- StringBuffer strBuf = new StringBuffer((int) entity.getContentLength());
- while ((line = reader.readLine()) != null) {
- strBuf.append(line);
- }
- strBuf.trimToSize();
- System.out.println(strBuf.toString());
- }
- if (entity != null) {
- entity.consumeContent();
- }
- }
- }
- }
对于显示将结果转换成String以备后续使用,HttpClient已经为我们提供了一个简便方法
如下
- System.out.println(EntityUtils.toString(entity,"utf-8"));
0 0
- HttpClient4基础1--通过匿名代理访问网页
- HttpClient4基础1--通过匿名代理访问网页
- HttpClient4基础1--通过匿名代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- C# webBrowser 通过代理访问网页
- 客户端通过ISA代理匿名访问FTP服务器问题解决
- C/C++程序实现通过http代理访问网页内容
- C/C++程序实现通过http代理访问网页内容
- HttpClient4.5.3设置代理(Proxy)访问目标地址
- 通过代理访问web
- perl 通过代理 访问
- 通过Intent访问网页
- 通过nginx代理静态网页
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- 通过匿名方式访问SAS存储过程
- db2 考完了的第3天
- Android 使用File ExPlorer无法访问系统内部文件data/data
- C++primer读书笔记6-类的细节知识
- 浙大复试机试 HDU 1879 继续畅通工程
- 英文原文:6 Life Habits That Programming Could Teach You Today
- HttpClient4基础1--通过匿名代理访问网页
- 万人感染研究用途口语课vgnh
- javaweb之web工程中各类地址的写法
- 用asp条调语句if then else end if 来定义文本框的值类,色彩,大小等
- HttpClient4基础2--通过认证代理访问网页
- Bash在cd进入目录时自动启动脚本
- HttpClient4 警告: Invalid cookie header 的问题解决
- HttpClient 4 设置超时
- 【蓝桥杯】剪格子