2013-02-19 2 views
0

인터넷에 많은 튜토리얼이 있다는 것을 알고 있지만 https를 처음 사용하기 때문에 지식의 범위를 벗어납니다. emmby의 답변은 여기 Trusting all certificates using HttpClient over HTTPS입니다. 그러나 나는 서버에 연결하는 클래스에서 어떻게 구현해야하는지 알지 못한다. 여기에 내 입술/원시 폴더에 * .bks 파일을 가지고 내가 거기에 붙어 내 HttpsConection 클래스https를 통한 자체 서명 된 인증서 신뢰하기

Log.d("url", url.toString()); 
     HttpsURLConnection httpsConnection; 

     Log.d("HTTP get", "get() called"); 
     try 
     { 
      Log.v("HttpConnection", url.toString()); 
      httpsConnection = (HttpsURLConnection) url.openConnection(); 

      if (request != null) 
      { 
       OutputStreamWriter wr = new OutputStreamWriter(
         httpsConnection.getOutputStream()); 
       // Log.e(TAG, "created outputstream"); 

       wr.write(request); 
       // Log.e(TAG, "request sent"); 
       wr.flush(); 
       wr.close(); 
      } else 
      { 
       Log.e("HttpConnection", "Nothing to send to server"); 
      } 

      // Execute 
      try 
      { 

       InputStream in = new BufferedInputStream(httpsConnection 
         .getInputStream()); 
       responseString = convertStreamToString(in); 
       in.close(); 

의 코드입니다.

답변

0

이 자체 서명 된 HTTPS 연결을위한 내 코드, 그것은 나를 위해 잘 작동 있습니다 :

 KeyStore trustStore; 
     try { 
      trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
      trustStore.load(null, null); 
      SSLSocketFactory sf = new EasySSLSocketFactory(trustStore); 
      sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
      BasicHttpParams params = new BasicHttpParams(); 
      HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 
      SchemeRegistry registry = new SchemeRegistry(); 
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      registry.register(new Scheme("https", sf, 443)); 
      ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 
      DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params); 
      HttpsURLConnection.setDefaultHostnameVerifier(sf.getHostnameVerifier()); 
      HttpGet getRequest = new HttpGet(url); 
      BasicHttpParams httpParameters = new BasicHttpParams(); 
      int timeoutConnection = 10000; 
      HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
      int timeoutSocket = 15000; 
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
      httpClient.setParams(httpParameters); 
      HttpResponse getResponse = httpClient.execute(getRequest); 
      final int statusCode = getResponse.getStatusLine().getStatusCode(); 
      if (statusCode != HttpStatus.SC_OK) 
      { 
       return null; 
      } 
      HttpEntity getResponseEntity = getResponse.getEntity(); 
      String content = EntityUtils.toString(getResponseEntity); 
      InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8")); 
      return is; 
     } catch (KeyStoreException e1) { 
      e1.printStackTrace(); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (CertificateException e) { 
      e.printStackTrace(); 
     } catch (KeyManagementException e) { 
      e.printStackTrace(); 
     } catch (UnrecoverableKeyException e) { 
      e.printStackTrace(); 
     } 
    return null; 
+0

이 모든 인증서를 허용하지 않습니다? – Bobans

+0

예, * sf.setHostnameVerifier (SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) * –

+0

에 의해 설정됩니다. 죄송 합니다만 하나의 인증서 만 허용하려고합니다. – Bobans