我正在尝试在Android平板电脑版本4.1.2上使用HTTPS:url连接到APi并收到错误消息“对等连接已关闭”
我想我的问题与TLS1.2有关,但是我已经尝试了几天,但没有成功。
我有用于API的c#代码,所以我只是查看url和参数是否正常,而在台式计算机上的c#中它运行正常。
我遵循以下代码: https://blog.dev-area.net/2015/08/13/android-4-1-enable-tls-1-1-and-tls-1-2/
制成:
public ApiTest() {
try {
String url = "https://example/oauth2/token";
HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory());
HttpsURLConnection con = (HttpsURLConnection) (new URL(url)).openConnection();
con.setSSLSocketFactory(new MySSLSocketFactory(con.getSSLSocketFactory()));
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML");
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Authorization", "aaaWc0VjRHdRa2Y2VElPRjBBZlB6TFdZWkpvOGJtQUE6dThrbURGZVVMNVpBZWlnQw==");
con.connect();
OutputStream output = con.getOutputStream();
} catch (Exception e) {
String r = e.getMessage();
}
}