gpt4 book ai didi

java - 代理仅忽略 httpS 请求的身份验证 header

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:40 26 4
gpt4 key购买 nike

我想通过代理发送两种请求:http 和 https。以下是我执行 HTTP 请求的方式:

  HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

和 HTTPS:

  HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(proxy);

其余代码相同:

//..................
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ip", 1234));

//..................
conn.setRequestProperty("Proxy-Connection", "Keep-Alive");
conn.setRequestProperty("Proxy-Authorization", auth);
conn.setDoInput(true);
System.out.println("Response Code : " + conn.getResponseCode());
System.out.println("\n");

对于 HTTP,它返回 200 和响应正文,这意味着一切正常。但是,对于 HTTPS 请求,它返回:

Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required

怎么会这样?

我不考虑使用 Authenticator 类或 System.setProperty 类。我想弄清楚为什么我的代码在 https 上无法正常工作,而在 http 上却无法正常工作。

最佳答案

您使用基本身份验证架构。
根据 Oracle 更新# 8u111

In some environments, certain authentication schemes may be undesirable when proxying HTTPS. Accordingly, the Basic authentication scheme has been deactivated, by default, in the Oracle Java Runtime, by adding Basic to the jdk.http.auth.tunneling.disabledSchemes networking property.

因此您需要明确的禁用方案列表
- 或在主静态方法中调用 System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
- 或添加JVM参数-Djdk.http.auth.tunneling.disabledSchemes=
- 或使用 Apache Common HttpClient

关于java - 代理仅忽略 httpS 请求的身份验证 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629004/

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