我正在尝试使用HTTPClient 4.1在restlet 2.1.7中通过代理获取https页面,并且它没有使用代理进行授权。如果我通过http连接到页面授权有效。通过代理连接httpurlconnect到同一个https站点也可以。
public class TestProxy {
public static void main(String[] args) {
Series<Parameter> parameters = null;
ClientResource clientResource = null;
Representation representation = null;
try {
Client client = new Client(new Context(), Protocol.HTTPS);
parameters = client.getContext().getParameters();
// proxy with credentials Works as excepted with http sites
parameters.add("proxyHost", PROXYIP);
parameters.add("proxyPort", PROXYPORT);
// create trust manager that trusts everything to eliminate
// certificates as an issue
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
// This will never throw an exception.
// This doesn't check anything at all: it's insecure.
}
};
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { tm }, null);
Context context = client.getContext();
context.getAttributes().put("sslContextFactory",
new SslContextFactory() {
public void init(Series<Parameter> parameters) {
}
public SSLContext createSslContext() {
return sslContext;
}
});
clientResource = new ClientResource("https://www.google.com");
clientResource.setProxyChallengeResponse(
ChallengeScheme.HTTP_BASIC, USER, PASS);
clientResource.setNext(client);
representation = clientResource.get();
System.out.println(representation.getText());
clientResource.get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后抛出407代理未授权的异常
Starting the Apache HTTP client
ThreadSafeClientConnManager.java:221) Get connection: HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com], timeout = 0
13 Jul 2015 10:18:11,747 DEBUG (ConnPoolByRoute.java:350) [HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com]] total kept alive: 0, total issued: 0, total allocated: 0 out of 20
13 Jul 2015 10:18:11,748 DEBUG (ConnPoolByRoute.java:523) No free connections [HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com]][null]
13 Jul 2015 10:18:11,749 DEBUG (ConnPoolByRoute.java:369) Available capacity: 10 out of 10 [HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com]][null]
13 Jul 2015 10:18:11,749 DEBUG (ConnPoolByRoute.java:549) Creating new connection [HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com]]
13 Jul 2015 10:18:11,769 DEBUG (DefaultClientConnectionOperator.java:145) Connecting to /PROXYIP:3128
13 Jul 2015 10:18:16,426 DEBUG (RequestAuthCache.java:75) Auth cache not set in the context
13 Jul 2015 10:18:16,426 DEBUG (DefaultClientConnection.java:264) Sending request: CONNECT www.google.com:443 HTTP/1.1
13 Jul 2015 10:18:16,428 DEBUG (Wire.java:63) >> "CONNECT www.google.com:443 HTTP/1.1[\r][\n]"
13 Jul 2015 10:18:16,430 DEBUG (Wire.java:63) >> "Host: www.google.com[\r][\n]"
13 Jul 2015 10:18:16,430 DEBUG (Wire.java:63) >> "Proxy-Connection: Keep-Alive[\r][\n]"
13 Jul 2015 10:18:16,431 DEBUG (Wire.java:63) >> "[\r][\n]"
13 Jul 2015 10:18:16,431 DEBUG (DefaultClientConnection.java:268) >> CONNECT www.google.com:443 HTTP/1.1
13 Jul 2015 10:18:16,432 DEBUG (DefaultClientConnection.java:271) >> Host: www.google.com
13 Jul 2015 10:18:16,432 DEBUG (DefaultClientConnection.java:271) >> Proxy-Connection: Keep-Alive
13 Jul 2015 10:18:16,455 DEBUG (Wire.java:63) << "HTTP/1.0 407 Proxy Authentication Required[\r][\n]"
13 Jul 2015 10:18:16,458 DEBUG (Wire.java:63) << "Server: squid/2.6.STABLE21[\r][\n]"
13 Jul 2015 10:18:16,459 DEBUG (Wire.java:63) << "Date: Mon, 13 Jul 2015 09:29:46 GMT[\r][\n]"
13 Jul 2015 10:18:16,459 DEBUG (Wire.java:63) << "Content-Type: text/html[\r][\n]"
13 Jul 2015 10:18:16,459 DEBUG (Wire.java:63) << "Content-Length: 1298[\r][\n]"
13 Jul 2015 10:18:16,460 DEBUG (Wire.java:63) << "Expires: Mon, 13 Jul 2015 09:29:46 GMT[\r][\n]"
13 Jul 2015 10:18:16,460 DEBUG (Wire.java:63) << "X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0[\r][\n]"
13 Jul 2015 10:18:16,461 DEBUG (Wire.java:63) << "Proxy-Authenticate: Basic realm="Squid proxy-caching web server"[\r][\n]"
13 Jul 2015 10:18:16,461 DEBUG (Wire.java:63) << "X-Cache: MISS from hostname.domain.tld[\r][\n]"
13 Jul 2015 10:18:16,461 DEBUG (Wire.java:63) << "X-Cache-Lookup: NONE from hostname.domain.tld:3128[\r][\n]"
13 Jul 2015 10:18:16,462 DEBUG (Wire.java:63) << "Via: 1.0 hostname.domain.tld:3128 (squid/2.6.STABLE21)[\r][\n]"
13 Jul 2015 10:18:16,462 DEBUG (Wire.java:63) << "Proxy-Connection: close[\r][\n]"
13 Jul 2015 10:18:16,462 DEBUG (Wire.java:63) << "[\r][\n]"
13 Jul 2015 10:18:16,464 DEBUG (DefaultClientConnection.java:249) Receiving response: HTTP/1.0 407 Proxy Authentication Required
13 Jul 2015 10:18:16,464 DEBUG (DefaultClientConnection.java:252) << HTTP/1.0 407 Proxy Authentication Required
13 Jul 2015 10:18:16,464 DEBUG (DefaultClientConnection.java:255) << Server: squid/2.6.STABLE21
13 Jul 2015 10:18:16,465 DEBUG (DefaultClientConnection.java:255) << Date: Mon, 13 Jul 2015 09:29:46 GMT
13 Jul 2015 10:18:16,465 DEBUG (DefaultClientConnection.java:255) << Content-Type: text/html
13 Jul 2015 10:18:16,465 DEBUG (DefaultClientConnection.java:255) << Content-Length: 1298
13 Jul 2015 10:18:16,466 DEBUG (DefaultClientConnection.java:255) << Expires: Mon, 13 Jul 2015 09:29:46 GMT
13 Jul 2015 10:18:16,466 DEBUG (DefaultClientConnection.java:255) << X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0
13 Jul 2015 10:18:16,466 DEBUG (DefaultClientConnection.java:255) << Proxy-Authenticate: Basic realm="Squid proxy-caching web server"
13 Jul 2015 10:18:16,467 DEBUG (DefaultClientConnection.java:255) << X-Cache: MISS from hostname.domain.tld
13 Jul 2015 10:18:16,467 DEBUG (DefaultClientConnection.java:255) << X-Cache-Lookup: NONE from hostname.domain.tld:3128
13 Jul 2015 10:18:16,467 DEBUG (DefaultClientConnection.java:255) << Via: 1.0 hostname.domain.tld:3128 (squid/2.6.STABLE21)
13 Jul 2015 10:18:16,468 DEBUG (DefaultClientConnection.java:255) << Proxy-Connection: close
13 Jul 2015 10:18:16,470 DEBUG (ResponseProcessCookies.java:78) Cookie spec not specified in HTTP context
13 Jul 2015 10:18:16,473 DEBUG (Wire.java:63) << "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">[\n]"
13 Jul 2015 10:18:16,474 DEBUG (Wire.java:63) << "<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">[\n]"
13 Jul 2015 10:18:16,474 DEBUG (Wire.java:63) << "<TITLE>ERROR: Cache Access Denied</TITLE>[\n]"
13 Jul 2015 10:18:16,475 DEBUG (Wire.java:63) << "<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>[\n]"
13 Jul 2015 10:18:16,475 DEBUG (Wire.java:63) << "</HEAD>[\n]"
13 Jul 2015 10:18:16,476 DEBUG (Wire.java:63) << "<BODY>[\n]"
13 Jul 2015 10:18:16,476 DEBUG (Wire.java:63) << "<H1>ERROR</H1>[\n]"
13 Jul 2015 10:18:16,477 DEBUG (Wire.java:63) << "<H2>Cache Access Denied</H2>[\n]"
13 Jul 2015 10:18:16,477 DEBUG (Wire.java:63) << "<HR noshade size="1px">[\n]"
13 Jul 2015 10:18:16,478 DEBUG (Wire.java:63) << "<P>[\n]"
13 Jul 2015 10:18:16,478 DEBUG (Wire.java:63) << "While trying to retrieve the URL:[\n]"
13 Jul 2015 10:18:16,479 DEBUG (Wire.java:63) << "<A HREF="www.google.com:443">www.google.com:443</A>[\n]"
13 Jul 2015 10:18:16,479 DEBUG (Wire.java:63) << "<P>[\n]"
13 Jul 2015 10:18:16,480 DEBUG (Wire.java:63) << "The following error was encountered:[\n]"
13 Jul 2015 10:18:16,480 DEBUG (Wire.java:63) << "<UL>[\n]"
13 Jul 2015 10:18:16,480 DEBUG (Wire.java:63) << "<LI>[\n]"
13 Jul 2015 10:18:16,481 DEBUG (Wire.java:63) << "<STRONG>[\n]"
13 Jul 2015 10:18:16,481 DEBUG (Wire.java:63) << "Cache Access Denied.[\n]"
13 Jul 2015 10:18:16,482 DEBUG (Wire.java:63) << "</STRONG>[\n]"
13 Jul 2015 10:18:16,482 DEBUG (Wire.java:63) << "</UL>[\n]"
13 Jul 2015 10:18:16,483 DEBUG (Wire.java:63) << "</P>[\n]"
13 Jul 2015 10:18:16,483 DEBUG (Wire.java:63) << "[\n]"
13 Jul 2015 10:18:16,483 DEBUG (Wire.java:63) << "<P>Sorry, you are not currently allowed to request:[\n]"
13 Jul 2015 10:18:16,484 DEBUG (Wire.java:63) << "<PRE> www.google.com:443</PRE>[\n]"
13 Jul 2015 10:18:16,484 DEBUG (Wire.java:63) << "from this cache until you have authenticated yourself.[\n]"
13 Jul 2015 10:18:16,485 DEBUG (Wire.java:63) << "</P>[\n]"
13 Jul 2015 10:18:16,485 DEBUG (Wire.java:63) << "[\n]"
13 Jul 2015 10:18:16,486 DEBUG (Wire.java:63) << "<P>[\n]"
13 Jul 2015 10:18:16,486 DEBUG (Wire.java:63) << "You need to use Netscape version 2.0 or greater, or Microsoft Internet[\n]"
13 Jul 2015 10:18:16,487 DEBUG (Wire.java:63) << "Explorer 3.0, or an HTTP/1.1 compliant browser for this to work. Please[\n]"
13 Jul 2015 10:18:16,487 DEBUG (Wire.java:63) << "contact the <A HREF="mailto:root">cache administrator</a> if you have[\n]"
13 Jul 2015 10:18:16,487 DEBUG (Wire.java:63) << "difficulties authenticating yourself or [\n]"
13 Jul 2015 10:18:16,488 DEBUG (Wire.java:63) << "<A HREF="http://hostname.domain.tld/cgi-bin/chpasswd.cgi">change</a> your default password.[\n]"
13 Jul 2015 10:18:16,488 DEBUG (Wire.java:63) << "</P>[\n]"
13 Jul 2015 10:18:16,488 DEBUG (Wire.java:63) << "[\n]"
13 Jul 2015 10:18:16,489 DEBUG (Wire.java:63) << "<BR clear="all">[\n]"
13 Jul 2015 10:18:16,489 DEBUG (Wire.java:63) << "<HR noshade size="1px">[\n]"
13 Jul 2015 10:18:16,489 DEBUG (Wire.java:63) << "<ADDRESS>[\n]"
13 Jul 2015 10:18:16,490 DEBUG (Wire.java:63) << "Generated Mon, 13 Jul 2015 09:29:46 GMT by hostname.domain.tld (squid/2.6.STABLE21)[\n]"
13 Jul 2015 10:18:16,490 DEBUG (Wire.java:63) << "</ADDRESS>[\n]"
13 Jul 2015 10:18:16,490 DEBUG (Wire.java:63) << "</BODY></HTML>[\n]"
13 Jul 2015 10:18:16,491 DEBUG (DefaultClientConnection.java:165) Connection closed
13 Jul 2015 10:18:16,491 DEBUG (DefaultRequestDirector.java:418) CONNECT refused by proxy: HTTP/1.0 407 Proxy Authentication Required
13 Jul 2015 10:18:16,492 DEBUG (ThreadSafeClientConnManager.java:272) Released connection is not reusable.
13 Jul 2015 10:18:16,492 DEBUG (ConnPoolByRoute.java:434) Releasing connection [HttpRoute[{tls}->http://PROXYIP:3128->https://www.google.com]][null]
13 Jul 2015 10:18:16,493 DEBUG (ConnPoolByRoute.java:679) Notifying no-one, there are no waiting threads
Proxy Authentication Required (407) - Proxy Authentication Required
at org.restlet.resource.ClientResource.doError(ClientResource.java:612)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1202)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1069)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1044)
at org.restlet.resource.ClientResource.handle(ClientResource.java:950)
at org.restlet.resource.ClientResource.get(ClientResource.java:658)
at TestProxy.main(TestProxy.java:68)