HttpsURLConnection wi proxy +How to use HttpsURLConnection through proxy by setProperty? URL connection and proxy : HttpURLConnection


Please briefly explain why you feel this question should be reported .


Example 1

Properties systemSettings = System.getProperties();
systemSettings.put("proxySet", "true");
systemSettings.put("http.proxyHost", "proxy.my_company.local");
systemSettings.put("http.proxyPort", "80");

URL url = new URL("");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
String encodedUserPwd = encoder.encode("domain\username:password".getBytes());
con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);


The Networking Properties

You have to set the following properties :

http.proxyHost (default: <none>)
http.proxyPort (default: 80 if http.proxyHost specified)
http.nonProxyHosts (default: <none>)

NOTE: proxyHostproxyPort are properties which are used to configure the proxy value. you have to prefix them with “http.”.
NOTE: Those properties are documented here

set the required properties of JVM for a JAVA application through the command line:

java -Dhttp.proxyPort=80 MyJavaApp

Or in your source :

System.setProperty(“http.proxyHost”, “”);
System.setProperty(“http.proxyPort”, “80”);

Since Java 1.5 you can also pass a instance to the openConnection() method:

//Proxy instance, proxy ip = with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy_server", port_number));
URL url = new URL("");
HttpURLConnection conn= (HttpURLConnection)url.openConnection(proxy);

To bypass the PROXY,

URL url = new URL("http://internal.server.local/");
URLConnection conn = url.openConnection(Proxy.NO_PROXY);

Proxy and Username/Password

Identification of you, you need to pass your credential as follows.

HTTP property “Proxy-Authorization” with a username:password base64 encoded.

System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "80");
URL url=new URL("http://domain_name/page_name");
URLConnection conn= url.openConnection ();
String encoded = new String
(Base64.base64Encode(new String("username:password").getBytes()));
conn.setRequestProperty("Proxy-Authorization", "Basic " + encoded);

NOTE: For a base64 function, see this How-to.

Manimaran Samuthirapandi 9 months 2021-08-11T15:05:31+02:00 0 Answers 4 views 0

Leave an answer

Captcha Click on image to update the captcha .