4

I have a code to perform POST Requests with HttpsUrlConnection, the code works fine, but some of my Users have SIM Cards with a closed Usergroup and they need to set a proxy in the settings of their apn. If they set the proxy, i need to modify my code. I Tryed 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 receive the error:

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

If i use url.OpenConnection() wuithout Proxy and without Proxysettings in the apn, the code works, what might be the Problem?

2red13
  • 11,197
  • 8
  • 40
  • 52

2 Answers2

3

You could try this alternative way of registering a proxy server:

Properties systemSettings=System.getProperties();

systemSettings.put("http.proxyHost", "your.proxy.host.here");
systemSettings.put("http.proxyPort", "8080"); // use actual proxy port
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    @CommonWare, I thought 3rd party apps cannot set proxy from their code because it does not have the permission. Proxy setting is reserved for system apps only.So does your suggestion really work? Is it for mobile network only or also applies to wifi? Thanks. – Safecoder Feb 04 '12 at 05:50
  • @HowardLi: My suggestion only affects your own app. – CommonsWare Feb 04 '12 at 11:47
  • @CommonsWare, Thanks for the clarification. It is quite confusing how Android handles proxy. I guess if I put something like this in my app, and the user set a different proxy in their wifi or mobile settings in Android, the two proxies will just be chained? I posted a more general questions on Android proxy http://stackoverflow.com/questions/9139080/android-proxy-wifi-vs-mobile Do you mind comment on that? – Safecoder Feb 04 '12 at 16:48
1

You can use the NetCipher library to get easy proxy setting and a modern TLS config when using Android's HttpsURLConnection. Call NetCipher.setProxy() to set the app-global proxy. NetCipher also configures the HttpsURLConnection instance to use the best supported TLS version, removes SSLv3 support, and configures the best suite of ciphers for that TLS version. First, add it to your build.gradle:

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

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

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

Call this:

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