When calling HttpURLConnection.getOutputStream() I get this IOException:
Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"
Code snippet:
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());
}
Produced output:
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
I tried random free proxies I found on the internet and all had the same output.
Is this something related to my computer? Or is my code for using proxies wrong?