Android HttpsUrlConnection javax.net.ssl.SSLException Connection closed by peer handshake error when using local truststore

后端 未结 2 1339
耶瑟儿~
耶瑟儿~ 2020-12-23 23:19

I\'m having trouble with getting Android to connect to a simple OpenSSL server using the HttpsUrlConnection object (I\'ve combed through StackOverf

2条回答
  •  粉色の甜心
    2020-12-24 00:17

    I wasted my 6 - 7 hours fixed this problem and finally it worked with

    public void URLConnection(String webUrl) throws IOException, NoSuchAlgorithmException, KeyManagementException {
            //TLSSocketFactory objTlsSocketFactory = new TLSSocketFactory();
            URL url = new URL(webUrl);
            HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            //urlConnection.setSSLSocketFactory(objTlsSocketFactory);
    
            int responseCode = urlConnection.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);
    
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
    
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
    
            //print result
            System.out.println(response.toString());
        }
    

    And it worked !!!!!!

提交回复
热议问题