gpt4 book ai didi

java - 禁用 HTTPS 连接的 SSL 证书验证?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:54 25 4
gpt4 key购买 nike

<分区>

当我想打开一个 HTTPS 连接时,我得到了 SSL 异常。如何以一种对此异常不敏感的方式设置 HttpURLConnection?

我的代码是:

private String getData() {
String response = null;
String connection = "https://www.kamalan.com/";

try {
URL url = new URL(connection);
Log.i(TAG, "Try to open: " + connection);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

int responseCode = conn.getResponseCode();
Log.i(TAG, "Response code is: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);

// get returned message and show it
response = strBuilder.toString();
Log.i("JSON returned by server:", response);
}

in.close();

} else {
Log.e(TAG, "Couldn't open connection in getResepiItems()");
}
} catch (SSLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return response;
}

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