android - 使用代理的 HttpsUrlConnection

标签 android web-applications ssl proxy

我有一个使用 HttpsUrlConnection 执行 POST 请求的代码,该代码工作正常,但我的一些用户拥有带有封闭用户组的 SIM 卡,他们需要在他们的 apn 设置中设置代理。如果他们设置代理,我需要修改我的代码。我试过这个:

    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();
}

现在我收到错误:

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

如果我在 apn 中使用 url.OpenConnection() wuithout Proxy 和 Proxysettings,代码可以工作,可能是什么问题?

最佳答案

您可以尝试这种注册代理服务器的替代方法:

Properties systemSettings=System.getProperties();

systemSettings.put("http.proxyHost", "your.proxy.host.here");
systemSettings.put("http.proxyPort", "8080"); // use actual proxy port

关于android - 使用代理的 HttpsUrlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8239437/

相关文章:

android - 统一: Colors are Brighter on Android Device

android - Proguard 回溯丢失的行号

android - 检测到缺少 Android 平台

sql - 通过 CSV 数据导入/提取批量创建用户帐户

Android Volley Https SSL 自签名和 Google Maps API

vb.net - 在 Windows Phone (VB.NET) 中设置 HTTPS 连接

android - 带抽屉导航的 PreferenceActivity 抛出空指针异常

html - 当通过应用程序脚本部署为 Web 应用程序时,任何知道该链接的人都可以在 Google 云端硬盘中查看图像,但不会在 IOS 的网络浏览器中显示

asp.net - 使用 IdentityServer 4 和 ASP.NET Web 应用程序 (.NET Framework)

firefox - Firefox 和 IE8 的 EV SSL 证书问题