4

我目前正在使用带有代理信息的获取请求:

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。

4

3 回答 3

1

这是一个使用 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();
于 2017-04-03T12:41:59.397 回答
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

于 2014-05-02T11:10:49.333 回答
0
Executor executor = Executor.newInstance()
        .auth(new HttpHost("myproxy", 8080), "username", "password")
        .authPreemptive(new HttpHost("myproxy", 8080));
Response response = executor.execute(<your reques>);
于 2018-05-31T12:11:16.400 回答