How to use HttpsURLConnection through proxy by setProperty?

孤街醉人 提交于 2019-11-28 09:53:06

Your URL connection is https whereas you are only setting the http proxy.

Try setting the https proxy.

//System.setProperty("https.proxySet", "true"); 
 System.setProperty("https.proxyHost",10.100.21.11);
 System.setProperty("https.proxyPort","443");

EDIT @EJP is correct. There is no https.proxySet .. I copied your original question and included in the answer.

You will need to create a Proxy object for it. Create one as below:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyServer, Integer.parseInt(proxyPort)));

Now use this proxy to create the HttpURLConnection object.

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(proxy);

If you have to set the credentials for the proxy, set the Proxy-Authorization request property:

String uname_pwd = proxyUsername + ":" + proxyPassword
String authString = "Basic " + new sun.misc.BASE64Encoder().encode(uname_pwd.getBytes())
connection.setRequestProperty("Proxy-Authorization", authString);

And finally, you connect:

connection.connect();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!