Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 使用代理的HttpsUrlConnection大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用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();
}

现在我收到错误

如果我在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全部内容,希望文章能够帮你解决android – 使用代理的HttpsUrlConnection所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。