java 在java中设置代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5320317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:37:36  来源:igfitidea点击:

set proxy in java

javaproxy

提问by Rozi

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.

我创建了一个服务,通过互联网发布一些东西,一切都很好。但是当我将它部署到我们的服务器时,我得到连接状态:403,禁止。我认为这是因为我们的服务器不允许在没有先登录的情况下直接访问互联网。我们必须首先使用我们的用户名/密码登录浏览器才能访问互联网。

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.

我注意到,如果我在服务器中登录并访问互联网,我部署的服务运行正常。但我认为这不切实际,因为在这种情况下,如果有人或我不先登录,我的服务将无法运行。

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.

我曾尝试在 java 代码中设置代理,但无济于事。有人可以帮我解决这个问题吗?这是我发布我的服务片段。

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());
            }
}

And my process goes to else block and gets response message 403

我的过程转到 else 块并获得响应消息 403

回答by Brent Worden

Try using System.setProperty(String, String) instead.

尝试使用 System.setProperty(String, String) 代替。

回答by Jake Sankey

This is how you set application-wide proxies..

这就是您设置应用程序范围代理的方式..

System.setProperty("https.proxyHost", "myproxy.domain.com"); 
System.setProperty("https.proxyPort", "myport"); 

NOTE: use http.proxyHostand http.proxyPortif you don't require https.

注意:如果您不需要 https,请使用http.proxyHosthttp.proxyPort

回答by Zeinab Ghaffarnasab

If you are using a proxy on your system... (I mean on your operating system) you can use this line of code:

如果您在系统上使用代理...(我的意思是在您的操作系统上),您可以使用以下代码行:

System.setProperty("java.net.useSystemProxies", "true");

回答by Stephen C

I think that the problem is that you are setting the properties too late. The HTTP request router code is likely to read those properties onceduring its initialization. If your code sets the properties after the HTTP request router has initialized, the new property values will have no effect ... unless you can cause the request router to reinitialize.

我认为问题在于您设置属性太晚了。HTTP 请求路由器代码可能会在其初始化期间读取一次这些属性。如果您的代码在 HTTP 请求路由器初始化后设置属性,则新属性值将无效……除非您可以使请求路由器重新初始化。

One way to guarantee that your proxy property settings will take effect is to set them on the java command line that launches the web server; e.g. with Tomcat on Linux, you can do this by setting the relevant "-D..." options in the JAVA_OPTSenvironment variable.

保证您的代理属性设置生效的一种方法是在启动 Web 服务器的 java 命令行上设置它们;例如,对于 Linux 上的 Tomcat,您可以通过在JAVA_OPTS环境变量中设置相关的“-D...”选项来做到这一点。