我正在尝试为我正在使用 HttpClientBuilder
发出的请求设置代理,如下所示:
CredentialsProvider credsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), usernamePasswordCredentials);
builder.useSystemProperties();
builder.setProxy(new HttpHost(proxyHost, proxyPort));
builder.setDefaultCredentialsProvider(credsProvider);
builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
建造者在哪里:
HttpClientBuilder builder = HttpClientBuilder.create();
但是,当我执行此请求时出现此异常:
java.lang.RuntimeException: org.apache.http.conn.UnsupportedSchemeException: http protocol is not supported
Caused by: org.apache.http.conn.UnsupportedSchemeException: http protocol is not supported
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:108) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.conn.BasicHttpClientConnectionManager.connect(BasicHttpClientConnectionManager.java:338) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:388) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) ~[httpclient-4.5.1.jar:4.5.1]
(为简洁起见缩短的例外)
由于这是一个 HTTP 代理,我不想将方案更改为 HTTPS,无论如何都行不通。我如何让这个工作?
原文由 jobin 发布,翻译遵循 CC BY-SA 4.0 许可协议
为什么会出现这个问题?
答: 这实际上是因为
you forget to register a connection socket factory for the 'http' scheme
。Plain
'http'
在'https'
可以使用隧道之前,必须使用方案来建立到代理本身的中间连接。出于操作目的,您可以尝试以下代码:
我还会为您的研究建议简单的代码。希望它能拯救你。
ClientExecuteProxy.java
您是否正在为 Cloudant DB 使用 CloudantClient java API?
答:
如果是,那么事实证明设置代理时 HTTP 的问题是我们这边的一个错误(对此感到抱歉)。我们
released 1.2.1
修复了这个问题。您可以从 这里 下载 jar 文件。 (收集自 mike-rhodes 的回答)更新
从 HTTP 身份验证,
默认情况下,httpclient 不会抢先提供凭证,
it will first create a HTTP request without authentication parameters
。这是设计使然,作为安全预防措施,也是规范的一部分。但是,如果您不重试连接,或者您连接到的任何地方都希望您在第一次连接时发送身份验证详细信息,这会导致问题。它还会导致请求出现额外延迟,因为您需要进行多次调用,并导致 401 出现在日志中。解决方法是使用身份验证缓存假装您已经连接到服务器一次。这意味着您只会进行一次 HTTP 调用。
归功于 Cetra
相关链接: