Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set proxy in java

Tags:

java

proxy

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

like image 673
Rozi Avatar asked Sep 13 '26 18:09

Rozi


2 Answers

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

like image 157
Brent Worden Avatar answered Sep 16 '26 07:09

Brent Worden


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.

like image 41
Jake Sankey Avatar answered Sep 16 '26 08:09

Jake Sankey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!