我有一個代碼執行POST請求HttpsUrlConnection,代碼工作正常,但我的一些用戶有一個封閉的用戶組的SIM卡,他們需要在他們的APN的設置中設置一個代理。如果他們設置代理,我需要修改我的代碼。我tryed這一點:HttpsUrlConnection與代理
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
如果我使用url.OpenConnection()wuithout代理和無Proxysettings在APN,代碼工作,可能是什麼問題?
作品,感謝°! – 2red13
@CommonWare,我認爲第三方應用程序不能從代碼中設置代理,因爲它沒有權限。代理設置僅用於系統應用程序。所以您的建議是否真的有效?僅適用於移動網絡還是適用於WiFi?謝謝。 – Safecoder
@HowardLi:我的建議隻影響你自己的應用程序。 – CommonsWare