我如何检查“System.setProperty”完成的代理设置?在Java中?
我正在开发一个Java应用程序,该应用程序将用于在专用环境上启动一些测试自动化脚本。 必要时也需要设置代理。 我开发了一个类,该类将用于配置代理设置,一旦从外部读取属性包含flag'enable_proxy'equals'true'。
以下是我的摘要代码:
package proxy;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.lang3.exception.ExceptionUtils;
public class ProxyConfiguration {
private static String proxyHost;
private static String proxyPort; //80 if it isn't provided
private static String nonProxyHosts;
private static String protocolType;
public static boolean flagProxy;
//Set method used to read the flag Proxy
public static boolean isProxyEnabled(Properties myPropertiesFile) {
flagProxy = Boolean.parseBoolean(myPropertiesFile.getProperty("ENABLE_PROXY").toLowerCase());
return flagProxy;
}
public static String getProxyHost() {
return proxyHost;
}
//Set method used to read the Proxy host from properties
public static void setProxyHost(Properties myPropertiesFile) {
ProxyConfiguration.proxyHost = myPropertiesFile.getProperty("PROXY_HOST");
}
public static String getProxyPort() {
return proxyPort;
}
//Set method used to read the Proxy port from properties
public static void setProxyPort(Properties myPropertiesFile) {
ProxyConfiguration.proxyPort = myPropertiesFile.getProperty("PROXY_PORT");
}
public static String getNonProxyHosts() {
return nonProxyHosts.replace(",", "|");
}
//Set method used to read the exception from properties
public static void setNonProxyHosts(Properties myPropertiesFile) {
ProxyConfiguration.nonProxyHosts = myPropertiesFile.getProperty("PROXY_EXCEPTION");
}
public static String getProtocolType() {
return protocolType;
}
//Set method used to read the exception from properties
public static void setProtocolType(Properties myPropertiesFile) {
ProxyConfiguration.protocolType = myPropertiesFile.getProperty("PROTOCOL_TYPE");
}
//Method used to set the Proxy settings on system
public static void setProxyConfiguration(Properties myPropertiesFile) {
try {
ProxyConfiguration.setProtocolType(myPropertiesFile);
ProxyConfiguration.setProxyHost(myPropertiesFile);
ProxyConfiguration.setProxyPort(myPropertiesFile);
ProxyConfiguration.setNonProxyHosts(myPropertiesFile);
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty(ProxyConfiguration.getProtocolType() +".proxyHost", ProxyConfiguration.getProxyHost());
System.setProperty(ProxyConfiguration.getProtocolType() +".proxyPort", ProxyConfiguration.getProxyPort());
boolean exceptions = ProxyConfiguration.getNonProxyHosts().isEmpty();
if(!exceptions) {
System.setProperty(ProxyConfiguration.getProtocolType() +".nonProxyHosts", ProxyConfiguration.getNonProxyHosts());
}
try {
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();
//return true;
} catch (Exception e) {
//return false;
}
} catch (Exception e) {
System.err.println("Si è verificato un errore nella setProxyConfiguration: ");
String exc = ExceptionUtils.getStackTrace(e);
System.err.println(exc);
System.exit(-1);
}
}
}
如何检查代理设置是否正确设置?
我还尝试使用本地计算机的命令提示符并启动follwing命令:
- netsh winhttp show show proxy,
但我收到:
Current WinHTTP proxy settings:
Direct access (no proxy server).
读取的属性为:
- protocol_type = http
- proxy_host = webcache.mydomain.com(这是一个示例 java网络)
- proxy_port = 8080
- proxy_exception =
感谢您的支持!
i'm developing an Java application that will be used to launch some Test Automation scripts on dedicated environment.
It is required to set also the proxy when necessary.
I developed a class that will be used to configure the proxy settings once a properties read from external contains the flag 'ENABLE_PROXY' equals 'True'.
Following is my snippet code:
package proxy;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import org.apache.commons.lang3.exception.ExceptionUtils;
public class ProxyConfiguration {
private static String proxyHost;
private static String proxyPort; //80 if it isn't provided
private static String nonProxyHosts;
private static String protocolType;
public static boolean flagProxy;
//Set method used to read the flag Proxy
public static boolean isProxyEnabled(Properties myPropertiesFile) {
flagProxy = Boolean.parseBoolean(myPropertiesFile.getProperty("ENABLE_PROXY").toLowerCase());
return flagProxy;
}
public static String getProxyHost() {
return proxyHost;
}
//Set method used to read the Proxy host from properties
public static void setProxyHost(Properties myPropertiesFile) {
ProxyConfiguration.proxyHost = myPropertiesFile.getProperty("PROXY_HOST");
}
public static String getProxyPort() {
return proxyPort;
}
//Set method used to read the Proxy port from properties
public static void setProxyPort(Properties myPropertiesFile) {
ProxyConfiguration.proxyPort = myPropertiesFile.getProperty("PROXY_PORT");
}
public static String getNonProxyHosts() {
return nonProxyHosts.replace(",", "|");
}
//Set method used to read the exception from properties
public static void setNonProxyHosts(Properties myPropertiesFile) {
ProxyConfiguration.nonProxyHosts = myPropertiesFile.getProperty("PROXY_EXCEPTION");
}
public static String getProtocolType() {
return protocolType;
}
//Set method used to read the exception from properties
public static void setProtocolType(Properties myPropertiesFile) {
ProxyConfiguration.protocolType = myPropertiesFile.getProperty("PROTOCOL_TYPE");
}
//Method used to set the Proxy settings on system
public static void setProxyConfiguration(Properties myPropertiesFile) {
try {
ProxyConfiguration.setProtocolType(myPropertiesFile);
ProxyConfiguration.setProxyHost(myPropertiesFile);
ProxyConfiguration.setProxyPort(myPropertiesFile);
ProxyConfiguration.setNonProxyHosts(myPropertiesFile);
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty(ProxyConfiguration.getProtocolType() +".proxyHost", ProxyConfiguration.getProxyHost());
System.setProperty(ProxyConfiguration.getProtocolType() +".proxyPort", ProxyConfiguration.getProxyPort());
boolean exceptions = ProxyConfiguration.getNonProxyHosts().isEmpty();
if(!exceptions) {
System.setProperty(ProxyConfiguration.getProtocolType() +".nonProxyHosts", ProxyConfiguration.getNonProxyHosts());
}
try {
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();
//return true;
} catch (Exception e) {
//return false;
}
} catch (Exception e) {
System.err.println("Si è verificato un errore nella setProxyConfiguration: ");
String exc = ExceptionUtils.getStackTrace(e);
System.err.println(exc);
System.exit(-1);
}
}
}
How can i check if the proxy settings are set correctly?
I tried also to used the command prompt of my local machine and launch the follwing command:
- netsh winhttp show proxy
but i receive:
Current WinHTTP proxy settings:
Direct access (no proxy server).
The properties read are:
- PROTOCOL_TYPE=http
- PROXY_HOST=webcache.mydomain.com (it is an example found on Java Networking)
- PROXY_PORT=8080
- PROXY_EXCEPTION=
Thanks for supporting!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论