407 代理身份验证错误 - Unirest Java 8

407 Proxy Authentication error - Unirest Java 8

大家好我很绝望所以问你们。 我正在尝试执行一个简单的 HTTP 请求,但使用我的代理时我收到 407 错误代码。 我使用 Unirest 和 Java 8.

 Unirest.config().proxy(host, port, usernameProxy, passwordProxy);   
 HttpResponse<JsonNode> response = Unirest.post(url).asJson();
 String body = response.getBody().toString();

就是这样,我的 url 是私有的,但我是这样写的:“https://myurl.com/?param1=param¶m2.... 它无代理工作,但我坚持使用代理。 非常感谢

似乎 代理服务器需要 Headers 中的代理凭据,而 Unirest 没有'似乎在传播。

header 必须明确包含 "Proxy-Authorization" 键才能开始握手。

String proxyCred= "user:password"; 
String baseCred= Base64.getEncoder().encodeToString(proxyCred.getBytes());
HttpHeaders headers = new HttpHeaders();
headers.add("Proxy-Authorization", "Basic " + baseCred); // the proxy server needs this

这个解决方案使用了Basic机制;它可能不起作用,因为代理可能需要另一种类型的身份验证。您可以通过阅读服务器响应中的 Proxy-Authenticate header 来知道是哪一个。

如果通信不安全(HTTP 而不是 HTTPS),您可以通过嗅探数据包来读取响应WireShark 等工具。找到 407 packet 后,您可以读取 Proxy-Authenticate 值并根据它修改您的授权方法。