Install proxy in java

I create a service that sends something over the Internet and everything is in order. But when I deploy it to our server, I get the connection status: 403, forbidden. I think this is because our server will not allow direct access to the Internet without logging in. We must first enter the browser with our username / password in order to access the Internet.

I notice that if I have a login and Internet access on the server, the service I'm deploying is working fine. But I do not think this is practical, because in this case my service will not work if someone or I do not sign up first.

I tried to install a proxy server in java code, but to no avail. Can someone help me with this problem? Here I publish my service fragment.

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 block 403

+3
3

System.setProperty(String, String).

+4

- .

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

: http.proxyHost http.proxyPort, https.

+3

, , . HTTP-, , . , HTTP- , ... .

One way to ensure that your proxy settings take effect is to install them on the java command line that launches the web server; for example with Tomcat on Linux, you can do this by setting the appropriate "-D ..." parameters in the environment variable JAVA_OPTS.

+1
source

All Articles