How to get url connection using proxy in java?

I am trying to create a url connection using a proxy at runtime. My code is below:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.10.100.100", 80)); HttpURLConnection connection = (HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy); 

But that does not work. Does anyone know why?

+11
java url proxy connection
Nov 16 '11 at 7:19
source share
2 answers

adding an answer to help future visitors

 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.10.100.100", 80)); HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "text/xml"); connection.setRequestProperty("Accept", "text/xml, application/xml"); connection.setRequestMethod("POST"); 
+13
Dec 19 '11 at 12:27
source share

dku.rajkumar does not work with me.

I try this and it works. But it takes double time.

 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.10.100.100", 80)); HttpURLConnection connection = (HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy); ((HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy)).getInputStream(); System.out.println(connection.usingProxy()); 

the result is correct

without ((HttpURLConnection)new URL("http://abc.abcd.com").openConnection(proxy)).getInputStream();

the result is false

+2
Dec 02 '13 at 15:11
source share



All Articles