我目前正在使用带代理信息的获取请求:
String result1 = Request.Get("_http://somehost/")
.version(HttpVersion.HTTP_1_1)
.connectTimeout(1000)
.socketTimeout(1000)
.viaProxy(new HttpHost("myproxy", 8080))
.execute().returnContent().asString();
结果是"需要代理身份验证"错误。我相信发出请求的服务器需要用户名和密码吗?如果是这样,我该如何添加该细节?我之前从未使用过Fluent API。
答案 0 :(得分:0)
您需要CredentialsProvider
。
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(username, password));
final HttpPost httppost = new HttpPost(uri);
httppost.setConfig(RequestConfig.custom().setProxy(proxy).build());
String line = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build() .execute(httppost).getStatusLine());
此外,4.3.1中存在影响身份验证的错误。它在4.3.2中修复。 https://issues.apache.org/jira/browse/HTTPCLIENT-1435
答案 1 :(得分:0)
以下是使用Fluent API的示例。执行程序可用于指定代理的凭据。
HttpHost proxyHost = new HttpHost("myproxy", 8080);
Executor executor = Executor.newInstance()
.auth(proxyHost, "username", "password");
String result1 = executor.execute(Request.Get("_http://somehost/")
.viaProxy(proxyHost))
.returnContent()
.asString();
答案 2 :(得分:0)
Executor executor = Executor.newInstance()
.auth(new HttpHost("myproxy", 8080), "username", "password")
.authPreemptive(new HttpHost("myproxy", 8080));
Response response = executor.execute(<your reques>);