无法从android发出https请求错误-连接已被对等方关闭
我正在尝试从android向api端点“”发送https请求。然而,不管我尝试了什么选择,我都会得到“与同龄人近距离的连接”,来自邮递员和浏览器的请求都可以正常工作无法从android发出https请求错误-连接已被对等方关闭,android,sslexception,Android,Sslexception,我正在尝试从android向api端点“”发送https请求。然而,不管我尝试了什么选择,我都会得到“与同龄人近距离的连接”,来自邮递员和浏览器的请求都可以正常工作 String webUrl = EndPointMethods.BASE_URL + EndPointMethods.CATEGORIES; URL url = new URL(webUrl); HttpsURLConnection urlConnection = (
String webUrl = EndPointMethods.BASE_URL + EndPointMethods.CATEGORIES;
URL url = new URL(webUrl);
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
try {
urlConnection.setSSLSocketFactory(new SSLSocketFactoryExtended());
} catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
catch (KeyManagementException e) { e.printStackTrace();}
urlConnection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
在进行Https调用之前调用此方法解决了问题,但是我仍然不了解实际问题是什么,以及此方法如何解决问题,有人可以向我解释此函数在解决当前问题方面实现了什么功能吗
private void trustEveryone(Context appContext) {
try{
ProviderInstaller.installIfNeeded(appContext);
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (
NoSuchAlgorithmException | GooglePlayServicesNotAvailableException | KeyManagementException | GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
}
谢谢使用后,我相信主机名中的下划线就是问题所在。看看哪一个确认了您在JDK方面的工作,并可以解释为什么其他工具没有受到影响。谢谢Toby,我现在有了更好的理解