gpt4 book ai didi

android - 使用代理的 HttpsUrlConnection

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:34 24 4
gpt4 key购买 nike

我有一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com