Java – How to add no Proxy in java for a given ip address as in mozilla

java

I am reading xml in java via url, this is my code:

String web="example.com";
URL url = new URL(web);
URLConnection conn = url.openConnection();

conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

writer.write(ufx);
writer.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
   answer.append(line);
}
writer.close();
reader.close();

} catch (Exception e) {
   e.printStackTrace();
}

return answer.toString();

My problem is that the web url that I am using is blocked, and I want to read data from the web via input stream. The web opens successfully in mozilla after removing no proxy in mozilla.
How do I achieve this in java ?

Best Answer

There are system properties which specify the proxy configuration used by java. You can pass them as command line arguments, or set them first thing in your application:

java -Dhttp.proxyHost=1.1.1.1 -Dhttp.proxyPort=1234 -jar somejar.jar

Note that there are more, and you can also set different proxy settings for different protocols like http, https, and you can also specify exceptions.

To define an exception (not to use proxy), you can use the http.nonProxyHosts system property, for example:

java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
    -Dhttp.nonProxyHosts="localhost|host.example.com" 

Check more info on Official Oracle documentation.