[英]Proxy settings in a java program
我试图通过eclipse中的java程序连接到wsdl生成的客户端的Web服务。 我通过代理服务器传递我的请求。 但似乎请求没有通过。 相同的代理设置在SoapUI上正常工作。 请在下面找到我设置的系统属性。
Properties props= new Properties(System.getProperties());
props.put("http.proxySet", "true");
props.put("http.proxyHost", "10.x.x.x");
props.put("http.proxyPort", "80");
props.put("http.proxyUser","domainName\\xxx");
props.put("http.proxyPassword","xxx");
Properties newprops = new Properties(props);
Java程序抛出异常java.net.UnknownHostException:
我错过了什么?
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun
我使用以下代码(它的工作原理):
String host = "10.x.x.x";
String port = "80";
System.out.println("Using proxy: " + host + ":" + port);
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
如果您使用HTTPS连接webservice,那么要设置的代理属性是
https.proxyHost
https.proxyPort
( http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html )
除了设置系统属性外,还可以使用java.net.Authenticator来设置代理配置。
final String authUser = "user";
final String authPassword = "password";
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
您可以使用System.setProperty()
设置代理
System.setProperty("http.proxyHost","122.183.139.107");
System.setProperty("http.proxyPort","8080");
如果你想删除
System.setProperty("http.proxyHost","");
System.setProperty("http.proxyPort","");
我能够通过使用以下代码来完成代理。
为Snehasish Code添加了一些新行
final String authUser = "username";
final String authPassword = "password";
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
url = new URL("http://www.somewebsite.com/sendmyrequest");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setFollowRedirects(true);
System.getProperties().put("http.proxyHost", "proxy.xyz.com");
System.getProperties().put("http.proxyPort", "portNumber");
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
System.getProperties().put("http.proxySet", "true");
我有同样的问题。 以下代码适用于我设置代理。
System.setProperty("java.net.useSystemProxies", "true");
http.proxySet
没有这样的属性。
您需要在使用任何HTTP URL之前设置其他属性,之后更改它们无效。
如果需要动态更改代理,请参阅java.net.Proxy。
'明文连接?' 意思是它所说的:你正在使用SSL到非SSL目标,可能是一个明文目标。
在eclipse IDE中,转到Window-> Preferences。 在左侧的小方框上写代理 。 您应该看到网络连接 ,在那里输入您将使用的请求的代理设置(HTTP应该足够)。 我相信,如果不在代码本身内部设置代理,这将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.