如何像在 mozilla 中那样在 java 中为给定的 ip 地址不添加代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25738159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add no Proxy in java for a given ip address as in mozilla
提问by MorganM
I am reading xml in java via url, this is my code:
我正在通过 url 在 java 中读取 xml,这是我的代码:
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 ?
我的问题是我使用的 web url 被阻止,我想通过输入流从 web 读取数据。在 mozilla 中删除任何代理后,web 在 mozilla 中成功打开。我如何在 java 中实现这一点?
回答by icza
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 使用的代理配置。您可以将它们作为命令行参数传递,或者在您的应用程序中首先设置它们:
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.
注意还有更多,你也可以为不同的协议如http、https设置不同的代理设置,也可以指定例外。
To define an exception (not to use proxy), you can use the http.nonProxyHosts
system property, for example:
要定义异常(不使用代理),您可以使用http.nonProxyHosts
系统属性,例如:
java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
-Dhttp.nonProxyHosts="localhost|host.example.com"
Check more info on Official Oracle documentation.
查看有关Oracle 官方文档的更多信息。
回答by nablex
Since you are not using a programmatic proxy, it is using the system properties:
由于您没有使用程序化代理,它使用系统属性:
- http.proxyHost
- http.proxyPort
- http.nonProxyHosts
- 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 wantto use a proxy or you don't wantto use one. Can you specify? Either way the properties can help you or you can look at URL.openConnection(Proxy)
在重读你的问题,我其实不知道你是否要使用代理,或者您不想使用一个。可以指定吗?无论哪种方式,属性都可以帮助您,或者您可以查看 URL.openConnection(Proxy)
回答by Mustafa sabir
Add following lines of code to your source :-
将以下代码行添加到您的源代码中:-
System.getProperties().put("http.proxyHost", "");
System.getProperties().put("http.proxyPort", "");
System.getProperties().put("http.proxyUser", "");
System.getProperties().put("http.proxyPassword", "");
Since you do not want to use proxy , you can provide blank values to the proxy settings
由于您不想使用代理,您可以为代理设置提供空白值
or you can do change settings using java control panel:-
或者您可以使用 java 控制面板更改设置:-
In the Java Control Panel, under the General tab, click on Network Settings. Select the Use Browser Settings checkbox. Click OK to save your changes.
在 Java 控制面板的常规选项卡下,单击网络设置。选择使用浏览器设置复选框。单击“确定”保存更改。