2 回答

TA贡献1789条经验 获得超8个赞
请检查第三方支持的 TLS 版本。我在连接苹果云网址时遇到了 Received fatal alert: decode_error。我可以通过将协议显式设置为 TLS 1.2 来修复它。

TA贡献1831条经验 获得超10个赞
请尝试以下代码 -
public class NetClientPost {
public static String getMapData(String latlong) {
String response = null;
try {
disableCertificateValidation();
URL urlForGetRequest = new URL(
"http://localhost:8080/server api url");
String readLine = null;
HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
conection.setRequestMethod("GET");
// conection.setRequestProperty("userId", "a1bcdef"); // set userId its a sample
// here
int responseCode1 = conection.getResponseCode();
if (responseCode1 == HttpURLConnection.HTTP_OK) {
BufferedReader in1 = new BufferedReader(new InputStreamReader(conection.getInputStream()));
StringBuffer response1 = new StringBuffer();
while ((readLine = in1.readLine()) != null) {
response1.append(readLine);
}
in1.close();
// print result
response = response1.toString();
System.out.println("JSON String Result " + response1.toString());
// GetAndPost.POSTRequest(response.toString());
} else {
System.out.println("GET NOT WORKED");
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return response;
}
public static void disableCertificateValidation() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
// Ignore differences between given hostname and certificate hostname
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加回答
举报