`

HttpClient4基础2--通过认证代理访问网页

阅读更多
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.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基础1--通过匿名代理访问网页

    本篇将重点介绍HttpClient4的基础用法,特别是如何通过匿名代理来访问网页。 首先,让我们了解什么是HTTP代理。在互联网通信中,代理服务器充当了客户端与目标服务器之间的中介,可以用于匿名访问、负载均衡、缓存...

    amber-oauth2-httpclient4-0.22-incubating.zip

    标签:amber-oauth2-httpclient4-0.22-incubating.zip,amber,oauth2,httpclient4,0.22,incubating.zip包下载,依赖包

    httpclient-4.4.1-API文档-中文版.zip

    赠送jar包:httpclient-4.4.1.jar; 赠送原API文档:httpclient-4.4.1-javadoc.jar; 赠送源代码:httpclient-4.4.1-sources.jar; 赠送Maven依赖信息文件:httpclient-4.4.1.pom; 包含翻译后的API文档:httpclient...

    commons-httpclient-3.0-rc4

    httpclient常用的jar包,便于大家使用

    httpclient5-fluent-5.0.3.jar

    这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用

    httpclient-4.5.13-API文档-中文版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5的jar包下载

    这里提到的四个jar包——httpcore-4.2.4,httpclient-4.2.5,httpclient-cache-4.2.5,httpmime-4.2.5,都是Apache HttpClient库的不同组件,用于支持HTTP通信和相关功能。 **httpcore-4.2.4.jar** 是HTTP Core模块...

    httpclient-4.5.12-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.12.jar; 赠送原API文档:httpclient-4.5.12-javadoc.jar; 赠送源代码:httpclient-4.5.12-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.12.pom; 包含翻译后的API文档:...

    httpclient-4.5.6-API文档-中文版.zip

    赠送jar包:httpclient-4.5.6.jar; 赠送原API文档:httpclient-4.5.6-javadoc.jar; 赠送源代码:httpclient-4.5.6-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.6.pom; 包含翻译后的API文档:httpclient...

    httpclient-4.5jar

    httpclient-4.5所需jar包,里面包含httpclient-...4.httpclient-4.5.jar 5.httpclient-cache-4.5.jar 6.httpclient-win-4.5.jar 7.httpcore-4.4.1.jar 8.httpmime-4.5.jar 9.jna-4.1.0.jar 10.jna-platform-4.1.0.jar

    amber-oauth2-httpclient4-0.22-incubating.jar

    标签:amber-oauth2-httpclient4-0.22-incubating.jar,amber,oauth2,httpclient4,0.22,incubating,jar包下载,依赖包

    httpclient-4.5.13-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.13.jar; 赠送原API文档:httpclient-4.5.13-javadoc.jar; 赠送源代码:httpclient-4.5.13-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.13.pom; 包含翻译后的API文档:...

    amber-oauth2-httpclient4-0.22-incubating.tar.gz

    标签:amber-oauth2-httpclient4-0.22-incubating.tar.gz,amber,oauth2,httpclient4,0.22,incubating.tar.gz包下载,依赖包

    httpclient 4.X版本下载有的带源码包

    2017-12-20 09:28 2,348,172 httpclient-4.5.2-javadoc.jar 2017-01-08 11:15 707,899 httpclient-4.5.2-sources.jar 2016-12-12 14:46 736,658 httpclient-4.5.2.jar 2018-03-28 17:13 747,794 httpclient-4.5.3....

    commons-httpclient-3.0-rc4.rar

    《Apache Commons HttpClient 3.0-rc4:深入解析与应用》 Apache Commons HttpClient 是一个广泛使用的Java库,专门用于实现HTTP客户端的功能。这个库在3.0-rc4版本中提供了许多改进和优化,使其成为开发人员进行...

    httpclient-4.5.2-API文档-中英对照版.zip

    赠送jar包:httpclient-4.5.2.jar; 赠送原API文档:httpclient-4.5.2-javadoc.jar; 赠送源代码:httpclient-4.5.2-sources.jar; 包含翻译后的API文档:httpclient-4.5.2-javadoc-API文档-中文(简体)-英语-对照...

    commons-httpclient-3.0.1--java网络开发

    package org.apache.commons.httpclient; // Imports import java.io.IOException; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.logging.Log; public class ...

    httpclient-4.5.5-API文档-中文版.zip

    赠送jar包:httpclient-4.5.5.jar; 赠送原API文档:httpclient-4.5.5-javadoc.jar; 赠送源代码:httpclient-4.5.5-sources.jar; 包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip ...

    wechatpay-apache-httpclient-0.2.1.jar

    wechatpay-apache-httpclient-0.2.1.jar

    httpclient-4.5.10-API文档-中文版.zip

    赠送jar包:httpclient-4.5.10.jar; 赠送原API文档:httpclient-4.5.10-javadoc.jar; 赠送源代码:httpclient-4.5.10-sources.jar; 赠送Maven依赖信息文件:httpclient-4.5.10.pom; 包含翻译后的API文档:...

Global site tag (gtag.js) - Google Analytics