【发布时间】:2020-04-18 13:45:45
【问题描述】:
当调用 HttpURLConnection.getOutputStream() 我得到这个 IOException:
无法通过代理建立隧道。代理返回“HTTP/1.1 400 错误请求”
代码 sn-p:
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
我尝试了在互联网上找到的随机免费代理,并且所有的输出都相同。
这与我的电脑有关吗?还是我使用代理的代码错了?
【问题讨论】:
标签: java proxy httpurlconnection ioexception bad-request