gpt4 book ai didi

authentication - Apache HttpClient 4.3.1 中使用 HTTP 隧道/HTTPS 连接的抢占式代理身份验证

转载 作者:行者123 更新时间:2023-12-04 19:07:24 24 4
gpt4 key购买 nike

我正在尝试通过需要使用 Apache HttpClient 4.3.1 进行抢占式身份验证的代理发送 HTTPS 请求。

当我没有在第一个请求中直接对自己进行身份验证时,我的代理会阻止来自我的 IP 的连接几分钟。

我对正常的 HTTP 请求没有任何问题,我只是在请求中手动添加了“代理授权” header 。

但是在尝试加载 HTTPS 页面时,HttpClient 似乎使用了 HTTP 隧道,因此第一个请求是“CONNECT”命令,然后发送我的实际请求。使用 request.setHeader(...) 方法不会影响 CONNECT 请求的 header ,从而导致“需要 HTTP/1.0 407 代理身份验证”响应并关闭我的连接。
之后,HttpClient 再次连接,这次使用我的凭据添加“Proxy-Authorization” header 字段。

连接成功(建立 HTTP/1.0 200 连接)并且我的实际 GET 请求正在执行。
但是当我在那之后再次运行我的程序时,我会得到一个 IOException:

Information: I/O exception (java.net.SocketException) caught when processing request: Connection reset



在 Wireshark 中,我可以看到代理不再响应我的“CONNECT”请求(不包含凭据)。
所以我尝试了几种方法让 HttpClient 在第一个 CONNECT 请求中发送凭据:
我改编了这个 example使用代理并为代理创建了 AuthCache,但它不起作用。
我还尝试向我的客户端添加一个 HttpRequestInterceptor:
static class PreemptiveAuth implements HttpRequestInterceptor {
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
request.setHeader("Proxy-Authorization", "Basic <base64credentials>");
}
}

但这也不会影响“CONNECT”请求。这是我的其余代码:
public class ClientProxyAuthentication {

public static void main(String[] args) throws IOException, InterruptedException {
HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
HttpHost proxy = new HttpHost("<proxy-ip>", 21265, "http");

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("<proxy-ip>", 21265),
new UsernamePasswordCredentials("username", "pass"));

CloseableHttpClient httpclient = HttpClients.custom()
.addInterceptorFirst(new PreemptiveAuth())
.setProxy(proxy)
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
.setDefaultCredentialsProvider(credsProvider).build();


try {

HttpGet httpget = new HttpGet("/");
httpget.setHeader("Proxy-Authorization", "Basic <base64credentials>");

System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost);

CloseableHttpResponse response = httpclient.execute(targetHost, httpget);
try {
HttpEntity entity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
String html = EntityUtils.toString(entity, "UTF-8");
System.out.println(html);
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

最佳答案

我怀疑您没有正确初始化身份验证缓存。请试试这个。

HttpHost proxy = new HttpHost("proxy", 8080);

BasicScheme proxyAuth = new BasicScheme();
// Make client believe the challenge came form a proxy
proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=default"));
BasicAuthCache authCache = new BasicAuthCache();
authCache.put(proxy, proxyAuth);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxy),
new UsernamePasswordCredentials("username", "password"));

HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context.setCredentialsProvider(credsProvider);

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
CloseableHttpResponse response = httpclient.execute(new HttpGet("/stuff"), context);
try {
// ...
} finally {
response.close();
}
} finally {
httpclient.close();
}

关于authentication - Apache HttpClient 4.3.1 中使用 HTTP 隧道/HTTPS 连接的抢占式代理身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com