在java中设置代理 [英] set proxy in java
问题描述
我创建了一个通过互联网发布内容的服务,一切都很好。但是当我将它部署到我们的服务器时,我得到连接状态:403,禁止。我认为这是因为我们的服务器不允许在没有登录的情况下直接访问互联网。我们必须首先在浏览器中使用我们的用户名/密码登录才能访问互联网。
I create a service that post something through the internet and everything's fine. But when I deploy it to our server I get connection status:403, forbidden. I think it is because our server won't allow direct access to the internet without login first. We have to login first in the browser with our username/password to access the internet.
我注意到如果我已登录并访问服务器中的互联网,该服务我部署运行正常。但我不认为这是实际的,因为在这种情况下,如果有人或我没有先登录,我的服务将无法运行。
I notice that if I have login and access the internet in the server, the service I deploy run alright. But I don't think it's practical because in that case my service won't run if someone or I don't login first.
我已尝试设置代理服务器java代码但无济于事。有人可以帮我解决这个问题吗?这是我发布我的服务片段。
I have tried setting the proxy in the java code but to no avail. Could someone help me with this problem? Here is I post my service snippet.
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", myHost);
System.getProperties().put("http.proxyPort", "8080");
System.getProperties().put("http.proxyUser", myUser);
System.getProperties().put("http.proxyPassword", myPassword);
System.getProperties().put("http.nonProxyHosts", "localhost|127.0.0.1");
try {
URL url = new URL(urlAddress);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
...
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("connection OK");
istrm = con.getInputStream();
if (istrm == null) {
System.out.println("istrm == null");
}
...
} else {
System.out.println("Response: " + con.getResponseCode() + ", " + con.getResponseMessage());
}
}
我的流程转到else阻止并获取响应消息403
And my process goes to else block and gets response message 403
推荐答案
请尝试使用System.setProperty(String,String)。
Try using System.setProperty(String, String) instead.
这篇关于在java中设置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!