0

I am trying to an https request from android to an api endpoint 'https://api_chamakah.ivilleinc.com/'. However regardless of what options I try I get "Connection close by peer", requests from post man and the browser work fine.

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

2 Answers2

0

Calling this method before making the Https call solved the problem, however I still dont understand what is the actual problem and how this method solves the problem, can someone explain to me what this function achieves in regards to the issue at hand

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

    }

Thanks

0

After using this tool, I believe the underscore in the hostname is the issue. See this thread which confirms your work-around for your JDK and may explain why other tools aren't impacted.

Toby
  • 186
  • 3