httpclient4.3如何设置代理服务器的用户名和密码

CommCsdn java开发  2015-10-19 02:35:09
httpclient4.3如何设置代理服务器的用户名和密码,要如何设置,在网上找了一些,都是3的版本的,有人知道怎么做么?
...全文
4910 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
myloverly 2017-04-26
引用 13 楼 net550503629 的回复:
[quote=引用 9 楼 CommCsdn 的回复:] 上下 我最后使用的代码,希望可以帮到有需要的人
//创建认证,并设置认证范围
			CredentialsProvider credsProvider = new BasicCredentialsProvider();
			credsProvider.setCredentials(new AuthScope(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT()),//可以访问的范围
					new UsernamePasswordCredentials(HsbcSmsUtilConfiguration.getPROXY_USERNAME(), HsbcSmsUtilConfiguration.getPROXY_PASSWORD()));//用户名和密码

			CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

			String responseText = "";
			CloseableHttpResponse response = null;

			// 访问的目标站点,端口和协议
			HttpHost targetHost = new HttpHost(HsbcSmsUtilConfiguration.getURI(), 443, "https");
			System.out.println(targetHost);

			// 代理的设置
			HttpHost proxy = new HttpHost(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT());
			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
找了半天也没找到靠谱的地方,最后没办法去阿帕奇的官网找了找,找到个demo,已测试成功,希望能帮助后面的人。[/quote]忘了把地址挂上了 https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
回复
myloverly 2017-04-26
引用 9 楼 CommCsdn 的回复:
上下 我最后使用的代码,希望可以帮到有需要的人
//创建认证,并设置认证范围
			CredentialsProvider credsProvider = new BasicCredentialsProvider();
			credsProvider.setCredentials(new AuthScope(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT()),//可以访问的范围
					new UsernamePasswordCredentials(HsbcSmsUtilConfiguration.getPROXY_USERNAME(), HsbcSmsUtilConfiguration.getPROXY_PASSWORD()));//用户名和密码

			CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

			String responseText = "";
			CloseableHttpResponse response = null;

			// 访问的目标站点,端口和协议
			HttpHost targetHost = new HttpHost(HsbcSmsUtilConfiguration.getURI(), 443, "https");
			System.out.println(targetHost);

			// 代理的设置
			HttpHost proxy = new HttpHost(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT());
			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
找了半天也没找到靠谱的地方,最后没办法去阿帕奇的官网找了找,找到个demo,已测试成功,希望能帮助后面的人。
回复
楼主能讲解一下么,我试了很多次,还是行不通。 例如:我的代理是proxy.future.com,端口8080 用户:xiaoli,密码:000000 调用的请求: HttpGet httpGet = new HttpGet("http://www.ip138.com:8080/search.asp?mobile=13000467124&action=mobile"); 理论上设置这四个数就行了,为什么有多个认证范围,我怎么调用都报错:java.net.UnknownHostException: localhost.localdomain 如果楼主方便的话,请帮我写一下代码,先谢谢了。
回复
xb4876 2016-04-08
我这代码还没测过 不知道对错啊!!
回复
xb4876 2016-04-08
对我帮助挺大的 谢谢!!

/**
	 * 设置代理
	 * @return
	 */
	public static CloseableHttpClient getHttpClient(String hostOrIP,int port,String userName,String password){
		//设置代理地址、代理端口号、协议类型
		HttpHost proxy = new HttpHost(hostOrIP,port,"http");
		DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
		//创建认证,并设置认证范围
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope(hostOrIP,port),new UsernamePasswordCredentials(userName, password));
		
		HttpClientBuilder builder = HttpClients.custom();
		builder.setRoutePlanner(routePlanner);
		builder.setDefaultCredentialsProvider(credsProvider);
		CloseableHttpClient httpclient = builder.build();
//		CloseableHttpClient httpclient = builder.create().build();
		return httpclient;
	}
回复
CommCsdn 2015-10-27
上下 我最后使用的代码,希望可以帮到有需要的人
//创建认证,并设置认证范围
			CredentialsProvider credsProvider = new BasicCredentialsProvider();
			credsProvider.setCredentials(new AuthScope(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT()),//可以访问的范围
					new UsernamePasswordCredentials(HsbcSmsUtilConfiguration.getPROXY_USERNAME(), HsbcSmsUtilConfiguration.getPROXY_PASSWORD()));//用户名和密码

			CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

			String responseText = "";
			CloseableHttpResponse response = null;

			// 访问的目标站点,端口和协议
			HttpHost targetHost = new HttpHost(HsbcSmsUtilConfiguration.getURI(), 443, "https");
			System.out.println(targetHost);

			// 代理的设置
			HttpHost proxy = new HttpHost(HsbcSmsUtilConfiguration.getPROXY_ADDRESS(), HsbcSmsUtilConfiguration.getPROXY_PORT());
			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
回复
兔子托尼啊 2015-10-20
客气,我也没用过4.3
回复
CommCsdn 2015-10-20
引用 6 楼 rui888 的回复:
你去网上找 4.3啊。
CloseableHttpClient httpclient = <...>

HttpHost targetHost = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
        new UsernamePasswordCredentials("username", "password"));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

HttpGet httpget = new HttpGet("/");
for (int i = 0; i < 3; i++) {
    CloseableHttpResponse response = httpclient.execute(
            targetHost, httpget, context);
    try {
        HttpEntity entity = response.getEntity();

    } finally {
        response.close();
    }
}
具体你再看看。
十分感谢您的耐心解答,昨天就找到了,和您的这个是一样的,十分感谢
回复
兔子托尼啊 2015-10-19
你去网上找 4.3啊。
CloseableHttpClient httpclient = <...>

HttpHost targetHost = new HttpHost("localhost", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
        new UsernamePasswordCredentials("username", "password"));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

HttpGet httpget = new HttpGet("/");
for (int i = 0; i < 3; i++) {
    CloseableHttpResponse response = httpclient.execute(
            targetHost, httpget, context);
    try {
        HttpEntity entity = response.getEntity();

    } finally {
        response.close();
    }
}
具体你再看看。
回复
CommCsdn 2015-10-19
回复
CommCsdn 2015-10-19
引用 3 楼 rui888 的回复:

client.getParams().setAuthenticationPreemptive(true);

Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);




参考
这个我i试过了,但是还是会报错,有一些类被弃用
回复
兔子托尼啊 2015-10-19

client.getParams().setAuthenticationPreemptive(true);

Credentials defaultcreds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(new AuthScope("myhost", 80, AuthScope.ANY_REALM), defaultcreds);

参考
回复
CommCsdn 2015-10-19
引用 1 楼 rui888 的回复:
System.getProperties().setProperty("proxySet", "true"); System.getProperties().setProperty("http.proxyHost", "xxxxxx"); System.getProperties().setProperty("http.proxyPort", "8080"); 这种不行吗
你帮我看下这个,public static String post(String url, Map<String, String> paramsMap) { { /* // yupian网地址 private static String URI = "yunpian.com"; //代理地址 private static String proxyAddress="10.0.0.122"; //代理地址的端口号 private static Integer proxyPort=1993;*/ // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); CloseableHttpClient client =httpClientBuilder.build(); String responseText = ""; CloseableHttpResponse response = null; // 访问的目标站点,端口和协议 HttpHost targetHost = new HttpHost(URI, 443, "https"); System.out.println(targetHost); // 代理的设置 HttpHost proxy = new HttpHost(proxyAddress, proxyPort); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); try { HttpPost method = new HttpPost(url); method.setConfig(config); if (paramsMap != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING)); } response = client.execute(targetHost, method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity); } } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (Exception e) { e.printStackTrace(); } } // System.out.println(responseText); return responseText; } } 上面的这段代码用了代理,但是现在想让代理有用户名和密码,不知道在那里设置,找了好久了,不知道怎么去做
回复
兔子托尼啊 2015-10-19
System.getProperties().setProperty("proxySet", "true"); System.getProperties().setProperty("http.proxyHost", "xxxxxx"); System.getProperties().setProperty("http.proxyPort", "8080"); 这种不行吗
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-10-19 02:35
社区公告
暂无公告