HttpClient4基础2--通过认证代理访问网页
2014-03-08 16:26
686 查看
http://llying.iteye.com/blog/456502
HttpClient发布4.0了 而且底层完全重写了,据说无论是效率还是结构都有质的飞跃。
现在也要与时具进,研究研究。
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201410/bd47857c2430372b4bd49574d4021af8.png)
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.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
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.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class GetHttpByProxyCredentials {
/**
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
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("proxy.tt", 8080);
//实例化验证
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//设定验证内容
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("fttj", "ft07");
//创建验证
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
creds);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
((DefaultHttpClient)httpClient).setCredentialsProvider(credsProvider);
// 目标地址
HttpGet httpget = new HttpGet("/");
// 执行
HttpResponse response = httpClient.execute(httpHost, httpget);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
//请求成功
//取得请求内容
HttpEntity entity = response.getEntity();
//显示内容
if (entity != null) {
// 显示结果
System.out.println(EntityUtils.toString(entity,"utf-8"));
}
if (entity != null) {
entity.consumeContent();
}
}
}
}
HttpClient发布4.0了 而且底层完全重写了,据说无论是效率还是结构都有质的飞跃。
现在也要与时具进,研究研究。
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201410/bd47857c2430372b4bd49574d4021af8.png)
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.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
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.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class GetHttpByProxyCredentials {
/**
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
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("proxy.tt", 8080);
//实例化验证
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//设定验证内容
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("fttj", "ft07");
//创建验证
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
creds);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
((DefaultHttpClient)httpClient).setCredentialsProvider(credsProvider);
// 目标地址
HttpGet httpget = new HttpGet("/");
// 执行
HttpResponse response = httpClient.execute(httpHost, httpget);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
//请求成功
//取得请求内容
HttpEntity entity = response.getEntity();
//显示内容
if (entity != null) {
// 显示结果
System.out.println(EntityUtils.toString(entity,"utf-8"));
}
if (entity != null) {
entity.consumeContent();
}
}
}
}
相关文章推荐
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础2--通过认证代理访问网页
- HttpClient4基础1--通过匿名代理访问网页
- HttpClient4基础1--通过匿名代理访问网页
- Httpclient通过代理访问网络
- HttpComponents(Apache HttpComponents Client 4.1.3)通过代理访问网页的设置方法
- HttpClient4基础1--通过匿名代理访问网页
- Httpclient通过代理访问网络
- Nginx实战基础篇五 如何让用户通过用户名密码认证访问我们的web站点
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
- java-httpclient通过title实现从baidu爬取相关网页链接
- 轻松把玩HttpClient之设置代理,可以访问FaceBook
- httpclient 访问网页面
- java-httpclient通过title实现从baidu爬取相关网页链接
- Nginx实战基础篇五 如何让用户通过用户名密码认证访问我们的web站点
- HttpClient通过代理进行请求
- Nginx 限制访问 - 通过 HTTP 基本认证限制访问
- C# webBrowser 通过代理访问网页