Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL connection using proxy in java?

I am trying to create URL connection using a proxy at run time. My code is below:

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

But this is not working. Anybody know why?

like image 626
dku.rajkumar Avatar asked Nov 16 '11 07:11

dku.rajkumar


People also ask

How do I get a proxy URL?

Proxy URL (-PROXY)Use the Proxy URL adapter command ( -PROXY ) to specify the URL to connect to a proxy server. You must specify a file name for the file to be transferred. Specify the user name to connect to the proxy server. Unless you are also specifying a password, this must be followed by @ if you specify host .

How do I make HttpURLConnection use a proxy?

Type. HTTP, new InetSocketAddress("10.0. 0.1", 8080)); conn = new URL(urlString). openConnection(proxy);


1 Answers

adding answer for the help of future visitors

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.0.2.100", 80));
HttpURLConnection connection =(HttpURLConnection)new URL("http://abc.example.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");
like image 82
dku.rajkumar Avatar answered Nov 09 '22 20:11

dku.rajkumar