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 ?
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.
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.
Since you are not using a programmatic proxy, it is using the system properties:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With