无法从 android 发出 https 请求错误 - 连接已被对等方关闭

Unable to make a https request from android error - Connection closed by peer

我正在尝试从 android 到 api 端点“https://api_chamakah.ivilleinc.com/”的 https 请求。但是,无论我尝试什么选项,我都会得到 "Connection close by peer",来自 post 人的请求和浏览器都可以正常工作。

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();
        }

    }

谢谢

在使用 this tool, I believe the underscore in the hostname is the issue. See 之后,它确认了您的 JDK 的解决方法,并且可以解释为什么其他工具没有受到影响。