HttpsUrl Proxy Connection

I have code for making POST requests using HttpsUrlConnection, the code works fine, but some of my users have SIM cards with a closed user group, and they need to install a proxy server in their apn settings. If they install a proxy server, I need to change the code. I tried this:

HttpsURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String urlServer = "https://xxx"; String boundary = "*****"; try { URL url = new URL(urlServer); SocketAddress sa = new InetSocketAddress("[MY PROXY HOST]",[My PROXY PORT]); Proxy mProxy = new Proxy(Proxy.Type.HTTP, sa); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;boundary=" + boundary); //this is supposed to open the connection via proxy //if i use url.openConnection() instead, the code works connection = (HttpsURLConnection) url.openConnection(mProxy); //the following line will fail outputStream = new DataOutputStream(connection.getOutputStream()); // [...] } catch (Exception ex) { ret = ex.getMessage(); } 

now I get the error message:

javax.net.ssl.SSLException: connection closed by peer

If I use url.OpenConnection () wuithout Proxy and without Proxysettings in apn, the code works, what could be the problem?

+4
source share
2 answers

You can try this alternative proxy registration method:

 Properties systemSettings=System.getProperties(); systemSettings.put("http.proxyHost", "your.proxy.host.here"); systemSettings.put("http.proxyPort", "8080"); // use actual proxy port 
+3
source

You can use the NetCipher library to get simple proxy settings and a modern TLS configurator when using Android HttpsURLConnection . Call NetCipher.setProxy() to set the global application proxy. NetCipher also configures the HttpsURLConnection instance to use the best supported version of TLS, removes SSLv3 support, and configures the best cipher suite for this version of TLS. First add it to your build.gradle:

 compile 'info.guardianproject.netcipher:netcipher:1.2' 

Or you can download netcipher-1.2.jar and include it directly in your application. Then instead of calling:

 HttpURLConnection connection = (HttpURLConnection) sourceUrl.openConnection(mProxy); 

Call:

 NetCipher.setProxy(mProxy); HttpURLConnection connection = NetCipher.getHttpURLConnection(sourceUrl); 
+1
source

Source: https://habr.com/ru/post/1382660/


All Articles