3

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.

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

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

Rozi
  • 35
  • 1
  • 1
  • 4

4 Answers4

4

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

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
3

This is how you set application-wide proxies..

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

NOTE: use http.proxyHost and http.proxyPort if you don't require https.

Jake Sankey
  • 4,977
  • 12
  • 39
  • 53
1

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");
1

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 once during 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.

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_OPTS environment variable.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216