System.getProperty("http.nonProxyHosts");结果永远是null
时间: 2024-02-01 12:35:57 浏览: 22
`System.getProperty("http.nonProxyHosts")` 是用来获取 Java 系统属性中 http.nonProxyHosts 的值的。如果返回值为 `null`,可能是因为该属性没有被设置或者该属性的值为空。
如果你希望设置 http.nonProxyHosts 的值,可以使用以下代码:
```java
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
```
这将设置 http.nonProxyHosts 的值为 "localhost|127.0.0.1",表示这些主机不会使用代理服务器。你可以将这个值设置为你需要的主机列表。
相关问题
windows有ie代理配置的例外列表,System.getProperty("http.nonProxyHosts")是null
`System.getProperty("http.nonProxyHosts")` 是 Java 的系统属性,用于设置不使用代理服务器的主机列表。它与 Windows 的 IE 代理配置的例外列表是不同的概念。
如果你想要获取 Windows 的 IE 代理配置的例外列表,你可以通过以下步骤进行:
1. 打开 Internet Explorer 浏览器
2. 点击菜单栏的“工具”选项
3. 选择“Internet 选项”
4. 在弹出的“Internet 选项”对话框中,点击“连接”选项卡
5. 点击“局域网设置”按钮
6. 在弹出的“局域网设置”对话框中,找到“代理服务器”部分下的“高级”按钮,并点击它
7. 在弹出的“高级”对话框中,你将会看到“在以下情况下不使用代理服务器”这一部分,里面列举了例外列表
如果你想要在 Java 中使用这个例外列表,可以使用 `java.net.ProxySelector` 类进行设置。例如:
```
String proxyHost = "your.proxy.host";
int proxyPort = 8080;
String nonProxyHosts = "<local>|localhost|127.*|[::1]";
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", String.valueOf(proxyPort));
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", String.valueOf(proxyPort));
System.setProperty("http.nonProxyHosts", nonProxyHosts);
System.setProperty("https.nonProxyHosts", nonProxyHosts);
ProxySelector.setDefault(new MyProxySelector());
```
这里的 `MyProxySelector` 是自定义的 `ProxySelector` 类,用于根据例外列表判断是否使用代理服务器。
String exceptionsList = System.getProperty("http.nonProxyHosts");获取不到
如果你的代码中使用 `System.getProperty("http.nonProxyHosts")` 方法获取不到数据,可能是因为该系统属性未被设置。你可以先检查一下该属性是否已经被设置,可以使用以下代码进行检查:
```
String exceptionsList = System.getProperty("http.nonProxyHosts");
if (exceptionsList == null) {
System.out.println("http.nonProxyHosts is not set");
} else {
System.out.println("http.nonProxyHosts is set to " + exceptionsList);
}
```
如果该属性确实未被设置,你可以通过以下方式设置该属性:
```
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
```
这样就可以将非代理主机设置为 localhost 和 127.0.0.1。你可以根据自己的需求修改这个字符串。