2017-04-21 10 views
0

にHTTPSで作業しながら、私は、HTTPSプロトコルSSLPeerUnverifiedExceptionを結果SSLPeerUnverifiedExceptionアンドロイドでアンドロイド

HttpResponse response = null; 
       try { 
        HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet(); 
        request.setURI(new URI(UrlConnectionConstants.SCI_TA_AGENT_URL)); 
        response = client.execute(request); 
       } catch (URISyntaxException e) { 
        e.printStackTrace(); 
       } catch (ClientProtocolException e) { 

        e.printStackTrace(); 
       } catch (IOException e) { 

        e.printStackTrace(); 
       } 
       return response; 

でRESTfulなAPIで働いています。ここで

+0

ための自己署名証明書のコードのサンプル片である、私はあなたが' http'接続と 'HttpsURLConnection'のための' HttpURLConnection'を使用することをお勧め'https'接続の場合 詳細情報http://stackoverflow.com/questions/22095813/androidhttpclient-versus-httpurlconnection-for-api-level-9-and-above#answer-22096707 – Deb

+0

「SSLPeerUnspicifiedException」はありません。あなたは 'SSLPeerUnverifiedException'を意味しますか? – Robert

+0

返信ありがとうございますが、HttpsURLConnectionクラスでも同じ問題が発生しています –

答えて

0

私は `HttpClient`は廃止されていると思いHTTPS接続

BufferedReader in = null; 
    HttpsURLConnection client=null; 
    try { 

     URL url = new URL(appendedUrl); 
     client= (HttpsURLConnection) url.openConnection(); 
     client.setRequestMethod("GET"); 
     client.setRequestProperty("Auth-Token", token); 
     client.setConnectTimeout(120000); 
     client.setReadTimeout(120000); 
     if(appendedUrl.contains("https")) { 
      client.setSSLSocketFactory(certificatePinning(context).getSocketFactory());//for certificate pinning 
      client.setHostnameVerifier(new HostnameVerifier() { 
       @Override 
       public boolean verify(String hostname, SSLSession session) { 
        if (hostname.equals("YOUR-HOST-ADDRESS")) 
         return true; 
        else 
         return false; 
       } 
      }); 
     } 
     try{ 
      in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
     }catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String result = sb.toString(); 
     return new JSONObject(result); 
    } 
    finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

さらに、この質問をチェックすることもできますhttp://stackoverflow.com/questions/9092857/sslpeerunverifiedexception-while-creating-an-ssl-connection-on-android?rq=1 – Deb