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

Report
Question

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

Report
Cancel

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("http://www.xxxxx.com");
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);
con.setRequestMethod("HEAD");
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.proxyHost=myproxyserver.com -Dhttp.proxyPort=80 MyJavaApp

Or in your source :

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

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

//Proxy instance, proxy ip = 123.0.0.1 with port 8080
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy_server", port_number));
URL url = new URL("http://www.xxxxx.com");
HttpURLConnection conn= (HttpURLConnection)url.openConnection(proxy);
conn.connect();

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", "my_Proxy_Server.com");
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);
conn.connect();

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

0
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 .