gpt4 book ai didi

java - Apache HttpClient 4.4 代理基本身份验证 : Multiple auth attemps

转载 作者:行者123 更新时间:2023-12-01 16:05:14 26 4
gpt4 key购买 nike

我正在尝试通过具有基本身份验证的代理使用 HTTP Client 4.4 发送请求。如果我输入正确的凭据,则连接成功。

但是,如果我输入错误的凭据,即使使用正确的凭据,以下所有其他连接尝试也将失败。

似乎一旦我们第一次提供了(错误的)凭据,新的(正确或错误的)凭据就永远不会发送。

您将看到有两个调用:

  1. 第一次通话,代理凭据错误
  2. 第二次调用,正确的代理凭据

代码:

package test;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

public class Main {

private HttpClient client;
private HttpClientContext context = HttpClientContext.create();

public static void main(String[] args) throws Exception {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "ERROR");

Main main = new Main();
main.createHttpClient();
main.send(true, "test", "wrong"); // 1st call, wrong credentials, if this call is removed, it works !
main.send(true, "test", "correct"); // 2nd call, correct credentials
}

public void send(String username, String password) throws Exception {
proxyAuthenticate(username, password);

HttpUriRequest request = new HttpGet("https://the-backend.xyz");
HttpResponse httpResponse = client.execute(request, context);
int statusCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("######################### " + statusCode);
}

public void createHttpClient() throws Exception {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setProxy(new HttpHost("proxy.the-proxy.xyz", 1234));
client = httpClientBuilder.build();
}

private void proxyAuthenticate(String username, String password) {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
AuthScope scope = new AuthScope("proxy.the-proxy.xyz", 1234);

CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(scope, credentials);

context.setCredentialsProvider(provider);
}

}

第一次尝试的日志(使用错误的代理信用连接:两个http请求,一个没有信用,一个有信用,都返回407,看起来合乎逻辑):

2015/10/07 13:39:02:502 CEST [WARN] BasicAuthCache - Unexpected I/O error while serializing auth scheme java.io.NotSerializableException: test.Main at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) at org.apache.http.impl.client.BasicAuthCache.put(BasicAuthCache.java:107) at test.Main.proxyAuthenticate(Main.java:109) at test.Main.send(Main.java:56) at test.Main2.main(Main2.java:30)

2015/10/07 13:39:02:502 CEST [DEBUG] RequestAddCookies - CookieSpec selected: default 2015/10/07 13:39:02:502 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 0 of 3; total allocated: 0 of 3] 2015/10/07 13:39:02:502 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 1][route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 1 of 3; total allocated: 1 of 3] 2015/10/07 13:39:02:502 CEST [DEBUG] MainClientExec - Opening connection {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443 2015/10/07 13:39:02:502 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connecting to proxy.the-proxy.xyz/123.45.67.890:1234 2015/10/07 13:39:07:530 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connection established 321.54.76.098:58216<->123.45.67.890:1234 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 >> CONNECT the-backend.xyz:443 HTTP/1.1 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 >> Host: the-backend.xyz 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 >> User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_51) 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << HTTP/1.1 407 Proxy Authentication Required 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Proxy-Authenticate: BASIC realm="InternetAccess" 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Cache-Control: no-cache 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Pragma: no-cache 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Content-Type: text/html; charset=utf-8 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Proxy-Connection: close 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Connection: close 2015/10/07 13:39:07:530 CEST [DEBUG] headers - http-outgoing-1 << Content-Length: 2916 2015/10/07 13:39:07:530 CEST [DEBUG] HttpAuthenticator - Authentication required 2015/10/07 13:39:07:530 CEST [DEBUG] HttpAuthenticator - proxy.the-proxy.xyz:1234 requested authentication 2015/10/07 13:39:07:530 CEST [DEBUG] ProxyAuthenticationStrategy - Authentication schemes in the order of preference: [Negotiate, Kerberos, NTLM, Digest, Basic] 2015/10/07 13:39:07:530 CEST [DEBUG] ProxyAuthenticationStrategy - Challenge for Negotiate authentication scheme not available 2015/10/07 13:39:07:530 CEST [DEBUG] ProxyAuthenticationStrategy - Challenge for Kerberos authentication scheme not available 2015/10/07 13:39:07:530 CEST [DEBUG] ProxyAuthenticationStrategy - Challenge for NTLM authentication scheme not available 2015/10/07 13:39:07:530 CEST [DEBUG] ProxyAuthenticationStrategy - Challenge for Digest authentication scheme not available 2015/10/07 13:39:07:530 CEST [DEBUG] HttpAuthenticator - Selected authentication options: [BASIC [complete=true]] 2015/10/07 13:39:07:530 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection 2015/10/07 13:39:07:530 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connecting to proxy.the-proxy.xyz/123.45.67.890:1234 2015/10/07 13:39:12:560 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connection established 321.54.76.098:58217<->123.45.67.890:1234 2015/10/07 13:39:12:560 CEST [DEBUG] HttpAuthenticator - Generating response to an authentication challenge using basic scheme 2015/10/07 13:39:12:560 CEST [DEBUG] headers - http-outgoing-1 >> CONNECT the-backend.xyz:443 HTTP/1.1 2015/10/07 13:39:12:560 CEST [DEBUG] headers - http-outgoing-1 >> Host: the-backend.xyz 2015/10/07 13:39:12:560 CEST [DEBUG] headers - http-outgoing-1 >> User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_51) 2015/10/07 13:39:12:560 CEST [DEBUG] headers - http-outgoing-1 >> Proxy-Authorization: Basic xXXxxXxXXXXX 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << HTTP/1.1 407 Proxy Authentication Required 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Proxy-Authenticate: BASIC realm="InternetAccess" 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Cache-Control: no-cache 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Pragma: no-cache 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Content-Type: text/html; charset=utf-8 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Proxy-Connection: close 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Connection: close 2015/10/07 13:39:12:561 CEST [DEBUG] headers - http-outgoing-1 << Content-Length: 2966 2015/10/07 13:39:12:561 CEST [DEBUG] HttpAuthenticator - Authentication required 2015/10/07 13:39:12:561 CEST [DEBUG] HttpAuthenticator - proxy.the-proxy.xyz:1234 requested authentication 2015/10/07 13:39:12:561 CEST [DEBUG] HttpAuthenticator - Authorization challenge processed 2015/10/07 13:39:12:561 CEST [DEBUG] HttpAuthenticator - Authentication failed 2015/10/07 13:39:12:561 CEST [DEBUG] ProxyAuthenticationStrategy - Clearing cached auth scheme for http://proxy.the-proxy.xyz:1234 2015/10/07 13:39:12:561 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection 2015/10/07 13:39:12:561 CEST [DEBUG] MainClientExec - CONNECT refused by proxy: HTTP/1.1 407 Proxy Authentication Required 2015/10/07 13:39:12:561 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection 2015/10/07 13:39:12:561 CEST [DEBUG] MainClientExec - Connection discarded 2015/10/07 13:39:12:561 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection 2015/10/07 13:39:12:561 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 1][route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 0 of 3; total allocated: 0 of 3]

################### 407

第二次尝试的日志(使用正确的代理信用连接:只有一个 http 请求,为什么没有第二个具有正确信用的请求???):

2015/10/07 13:39:17:585 CEST [WARN] BasicAuthCache - Unexpected I/O error while serializing auth scheme java.io.NotSerializableException: test.Main at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) at org.apache.http.impl.client.BasicAuthCache.put(BasicAuthCache.java:107) at test.Main.proxyAuthenticate(Main.java:109) at test.Main.send(Main.java:56) at test.Main2.main(Main2.java:32)

2015/10/07 13:39:17:585 CEST [DEBUG] RequestAddCookies - CookieSpec selected: default 2015/10/07 13:39:17:585 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 0 of 3; total allocated: 0 of 3] 2015/10/07 13:39:17:585 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 2][route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 1 of 3; total allocated: 1 of 3] 2015/10/07 13:39:17:585 CEST [DEBUG] MainClientExec - Opening connection {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443 2015/10/07 13:39:17:585 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connecting to proxy.the-proxy.xyz/123.45.67.890:1234 2015/10/07 13:39:17:585 CEST [DEBUG] DefaultHttpClientConnectionOperator - Connection established 321.54.76.098:58218<->123.45.67.890:1234 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 >> CONNECT the-backend.xyz:443 HTTP/1.1 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 >> Host: the-backend.xyz 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 >> User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_51) 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << HTTP/1.1 407 Proxy Authentication Required 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Proxy-Authenticate: BASIC realm="InternetAccess" 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Cache-Control: no-cache 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Pragma: no-cache 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Content-Type: text/html; charset=utf-8 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Proxy-Connection: close 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Connection: close 2015/10/07 13:39:17:586 CEST [DEBUG] headers - http-outgoing-2 << Content-Length: 2916 2015/10/07 13:39:17:586 CEST [DEBUG] HttpAuthenticator - Authentication required 2015/10/07 13:39:17:586 CEST [DEBUG] HttpAuthenticator - proxy.the-proxy.xyz:1234 requested authentication 2015/10/07 13:39:17:586 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-2: Close connection 2015/10/07 13:39:17:586 CEST [DEBUG] MainClientExec - CONNECT refused by proxy: HTTP/1.1 407 Proxy Authentication Required 2015/10/07 13:39:17:586 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-2: Close connection 2015/10/07 13:39:17:586 CEST [DEBUG] MainClientExec - Connection discarded 2015/10/07 13:39:17:586 CEST [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-2: Close connection 2015/10/07 13:39:17:586 CEST [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 2][route: {tls}->http://proxy.the-proxy.xyz:1234->https://the-backend.xyz:443][total kept alive: 0; route allocated: 0 of 3; total allocated: 0 of 3]

################### 407

知道为什么在第二次尝试时,它在没有身份验证的情况下尝试(正常),服务器回复需要基本身份验证,然后它没有尝试使用正确的凭据,而是关闭连接。

谢谢!

更新:

如果我将 HttpClient(从 4.4 开始)更改为 DefaultHttpClient(从 4.3 开始)并调整这两种方法,它就可以工作。请注意,getCredentialsProvider 在 4.3 中是从客户端调用的,在 4.4 中是从上下文调用的!!!

第一次收到 407 状态代码,但第二次收到 200,这正是我所期望的。

public void createHttpClient() throws Exception {
client = new DefaultHttpClient();
HttpHost proxy = new HttpHost("proxy.eurocontrol.be", 9513);
ConnRouteParams.setDefaultProxy(client.getParams(), proxy);
}

private void proxyAuthenticate(String username, String password) {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
AuthScope scope = new AuthScope("proxy.eurocontrol.be", 9513);
client.getCredentialsProvider().setCredentials(scope, credentials);
// Called from client !!!!!!!!!!!!!!!!!
}

最佳答案

代码的第一行清楚地表明这两个请求均已发出。

First at  - 2015/10/07 13:39:02:502

Second at - 2015/10/07 13:39:17:585

您打印的是服务器响应的状态代码,参见thisHttp Status 407

甚至你的日志也清楚地说

Unexpected I/O error while serializing auth scheme java.io.NotSerializableException

由于序列化也可能出现异常。

关于java - Apache HttpClient 4.4 代理基本身份验证 : Multiple auth attemps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991611/

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