如何修复java.io.IOException:无法通过代理隧道。代理返回"HTTP/1.1 400 Bad Request" ?



当调用HttpURLConnection.getOutputStream((时,我得到这个IOException:

无法通过代理隧道。代理返回"HTTP/1.1 400 错误请求">

代码片段:

public static void main(String[] args) throws MalformedURLException, IOException {
HttpURLConnection conn = (HttpURLConnection) new URL("https://example.com")
.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("88.198.50.103", 3128)));
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os)) {
osw.write("Hello");
osw.flush();
} catch (IOException e) {
System.err.println("Caught exception while opening outputstream.");
e.printStackTrace();
}
System.out.println("Response: " + conn.getResponseMessage());
}

生产输出:

Caught exception while opening outputstream.
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at application.Main.main(Main.java:31)
Response: Bad Request

我尝试了在互联网上找到的随机免费代理,并且都具有相同的输出。

这与我的电脑有关吗?还是我使用代理的代码是错误的?

SocketAddress addr = new InetSocketAddress("104.248.63.17",30588);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
URL url = new URL("https://www.proxy-listen.de/Proxy/Proxyliste.html");
URLConnection   conn = url.openConnection(proxy);
//follow code reads html from url 
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();

这段代码对我有用。我在 https://www.proxy-listen.de/Proxy/Proxyliste.html 上搜索了socks5代理,然后它适用于类型袜子。

我遇到了同样的问题。我认为如果代理服务器代表高级协议(如 HTTP 或 FTP(的代理,它将不起作用。尝试更改为 Proxy.Type.SOCKS。

我发现这可能是旧JDK版本(包括1.4或1.6(中的错误,但是我使用JDK 8。

最新更新