Reputation: 59
When calling HttpURLConnection.getOutputStream()
via an HTTP proxy 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?
Upvotes: 3
Views: 10790
Reputation: 1
I encountered the same problem. I think it will not work if the proxy server represents a proxy for high level protocols such as HTTP or FTP. Try changing to Proxy.Type.SOCKS.
I discovered that it may be a bug in old JDK versions including 1.4 or 1.6, however I use JDK 8.
Upvotes: -1
Reputation: 1
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();
This code worked for me. I searched on https://www.proxy-listen.de/Proxy/Proxyliste.html for an socks5 proxy and then it works fine with type socks.
Upvotes: -1