URL Connection Through Proxy in Java
Sometimes we want to connect to remote URL from our java program but we are unable to connect due to proxy server. So in this article we will be learning:
- How to set the Proxy Connection Settings in Java.
- How to connect to remote URL using Proxy Settings defined using Java.
Setting Proxy in Java:
Proxy can be set in java in System Properties. The following set of code will help in setting system properties.
Properties systemSettings = System.getProperties(); systemSettings.put("http.proxyPort","1000"); //1000 is your proxy port systemSettings.put("http.proxyHost","110.11.12.30"); //110.11.12.30 is your proxy port
Connect to Remote URL with Proxy in Java:
Now we are all set to connect to remote URL using proxy connection. Here i am using URLConnection class to connect to remote url.
Properties systemSettings = System.getProperties(); URLConnection urlconn = null; BufferedReader buffreader = null; String urlResponse = ""; String xmlResp = ""; systemSettings.put("http.proxyPort","1000"); //1000 is your proxy port systemSettings.put("http.proxyHost","110.11.12.30"); //110.11.12.30 is your proxy port URL address = new URL("www.google.com"); urlconn = address.openConnection(); urlconn.connect(); buffreader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); urlResponse = buffreader.readLine(); while (urlResponse!=null) { xmlResp+= urlResponse; urlResponse = buffreader.readLine(); } System.out.println("Response received from remote URL:" + xmlResp);
As you can see that before doing any remote connection i am setting the proxyHost and proxyPort in the system properties and than i am connecting to google.com. Finally i am reading the remote content in xmlResp variable.
Custom Search
















I liked your website…content-ful…!!!
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
Susan
http://8080proxy.com