[英]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.