我有一段代码,用于使用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: 连接被对等方关闭
如果我在apn中不使用代理和代理设置,直接使用url.OpenConnection(),那么代码可以正常运行,可能是什么问题呢?