0

我正在尝试从 android 到 api 端点“ https://api_chamakah.ivilleinc.com/ ”的 https 请求。但是,无论我尝试什么选项,我都会得到“通过对等方关闭连接”,来自邮递员和浏览器的请求都可以正常工作。

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

2 回答 2

0

使用此工具后,我认为主机名中的下划线是问题所在。请参阅此线程,该线程确认您的 JDK 解决方法,并可能解释为什么其他工具不受影响。

于 2019-10-07T06:04:47.507 回答
0

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

    }

谢谢

于 2019-10-06T14:25:46.260 回答