Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 ?

like image 469
MorganM Avatar asked Sep 09 '14 06:09

MorganM


People also ask

How do I configure proxy settings for Java?

Configure Proxies through the Java Control PanelIn the Java Control Panel, under the General tab, click on Network Settings. Select the Use Browser Settings checkbox. Click OK to save your changes. Close all browser windows.


2 Answers

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.

like image 188
icza Avatar answered Nov 11 '22 17:11

icza


Since you are not using a programmatic proxy, it is using the system properties:

  • http.proxyHost
  • http.proxyPort
  • http.nonProxyHosts

You can either not set them or update the last one.

UPDATE

Upon rereading your question I'm actually not sure whether you want to use a proxy or you don't want to use one. Can you specify? Either way the properties can help you or you can look at URL.openConnection(Proxy)

like image 28
nablex Avatar answered Nov 11 '22 17:11

nablex