偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

android httpClient 支持HTTPS的訪問方式

移動(dòng)開發(fā) Android
項(xiàng)目中Android https請求地址遇到了這個(gè)異常,javax.net.ssl.SSLPeerUnverifiedException: No peer certificate,是SSL協(xié)議中沒有終端認(rèn)證。

項(xiàng)目中Android https請求地址遇到了這個(gè)異常(無終端認(rèn)證):
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

是SSL協(xié)議中沒有終端認(rèn)證。

沒有遇到過的問題,于是無奈的去找度娘。。。。。。。

看了不少大神的博客后得到的解決方案如下:

  1. /** 
  2.   * Post請求連接Https服務(wù) 
  3.   * @param serverURL  請求地址 
  4.   * @param jsonStr    請求報(bào)文 
  5.   * @return 
  6.   * @throws Exception 
  7.   */ 
  8.  public static synchronized String doHttpsPost(String serverURL, String jsonStr)throws Exception { 
  9.      // 參數(shù) 
  10.      HttpParams httpParameters = new BasicHttpParams(); 
  11.      // 設(shè)置連接超時(shí) 
  12.      HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
  13.      // 設(shè)置socket超時(shí) 
  14.      HttpConnectionParams.setSoTimeout(httpParameters, 3000); 
  15.      // 獲取HttpClient對象 (認(rèn)證) 
  16.      HttpClient hc = initHttpClient(httpParameters); 
  17.      HttpPost post = new HttpPost(serverURL); 
  18.      // 發(fā)送數(shù)據(jù)類型 
  19.      post.addHeader("Content-Type""application/json;charset=utf-8"); 
  20.      // 接受數(shù)據(jù)類型 
  21.      post.addHeader("Accept""application/json"); 
  22.      // 請求報(bào)文 
  23.      StringEntity entity = new StringEntity(jsonStr, "UTF-8"); 
  24.      post.setEntity(entity); 
  25.      post.setParams(httpParameters); 
  26.      HttpResponse response = null
  27.      try { 
  28.          response = hc.execute(post); 
  29.      } catch (UnknownHostException e) { 
  30.          throw new Exception("Unable to access " + e.getLocalizedMessage()); 
  31.      } catch (SocketException e) { 
  32.          e.printStackTrace(); 
  33.      } 
  34.      int sCode = response.getStatusLine().getStatusCode(); 
  35.      if (sCode == HttpStatus.SC_OK) { 
  36.          return EntityUtils.toString(response.getEntity()); 
  37.      } else 
  38.          throw new Exception("StatusCode is " + sCode); 
  39.  } 
  40.  
  41.  private static HttpClient client = null
  42.  /** 
  43.   * 初始化HttpClient對象 
  44.   * @param params 
  45.   * @return 
  46.   */ 
  47.  public static synchronized HttpClient initHttpClient(HttpParams params) { 
  48.      if(client == null){ 
  49.          try { 
  50.              KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
  51.              trustStore.load(nullnull); 
  52.                
  53.              SSLSocketFactory sf = new SSLSocketFactoryImp(trustStore); 
  54.              //允許所有主機(jī)的驗(yàn)證 
  55.              sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
  56.                
  57.              HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
  58.              HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 
  59.              // 設(shè)置http和https支持 
  60.              SchemeRegistry registry = new SchemeRegistry(); 
  61.              registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
  62.              registry.register(new Scheme("https", sf, 443)); 
  63.                
  64.              ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 
  65.                
  66.              return new DefaultHttpClient(ccm, params); 
  67.          } catch (Exception e) { 
  68.              e.printStackTrace(); 
  69.              return new DefaultHttpClient(params); 
  70.          } 
  71.      } 
  72.      return client; 
  73.  } 
  74.  
  75. public static class SSLSocketFactoryImp extends SSLSocketFactory { 
  76.      final SSLContext sslContext = SSLContext.getInstance("TLS"); 
  77.  
  78.      public SSLSocketFactoryImp(KeyStore truststore) 
  79.              throws NoSuchAlgorithmException, KeyManagementException, 
  80.              KeyStoreException, UnrecoverableKeyException { 
  81.          super(truststore); 
  82.  
  83.          TrustManager tm = new X509TrustManager() { 
  84.              public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
  85.                  return null
  86.              } 
  87.  
  88.              @Override 
  89.              public void checkClientTrusted( 
  90.                      java.security.cert.X509Certificate[] chain, 
  91.                      String authType) 
  92.                      throws java.security.cert.CertificateException { 
  93.              } 
  94.  
  95.              @Override 
  96.              public void checkServerTrusted( 
  97.                      java.security.cert.X509Certificate[] chain, 
  98.                      String authType) 
  99.                      throws java.security.cert.CertificateException { 
  100.              } 
  101.          }; 
  102.          sslContext.init(nullnew TrustManager[] { tm }, null); 
  103.      } 
  104.  
  105.      @Override 
  106.      public Socket createSocket(Socket socket, String host, int port, 
  107.              boolean autoClose) throws IOException, UnknownHostException { 
  108.          return sslContext.getSocketFactory().createSocket(socket, host, 
  109.                  port, autoClose); 
  110.      } 
  111.  
  112.      @Override 
  113.      public Socket createSocket() throws IOException { 
  114.          return sslContext.getSocketFactory().createSocket(); 
  115.      } 
  116.  } 

run下,小手發(fā)抖的點(diǎn)到測試按鈕,深吸口氣,咦?沒反應(yīng)。。。馬蛋的,工作線程忘記start(),唉,再次run下,終于的有點(diǎn)反應(yīng)了,神奇的竟然沒有報(bào)之前的 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 的異常了。服務(wù)端的數(shù)據(jù)正常返回了。

分析問題:
HTTPS:超文本安全傳輸協(xié)議,和HTTP相比,多了一個(gè)SSL/TSL的認(rèn)證過程,端口為443。

1.peer終端發(fā)送一個(gè)request,https服務(wù)端把支持的加密算法等以證書的形式返回一個(gè)身份信息(包含ca頒發(fā)機(jī)構(gòu)和加密公鑰等)。

2.獲取證書之后,驗(yàn)證證書合法性。

3.隨機(jī)產(chǎn)生一個(gè)密鑰,并以證書當(dāng)中的公鑰加密。

4.request https服務(wù)端,把用公鑰加密過的密鑰傳送給https服務(wù)端。

5.https服務(wù)端用自己的密鑰解密,獲取隨機(jī)值。

6.之后雙方傳送數(shù)據(jù)都用此密鑰加密后通信。

HTTPS流程清楚后,問題也就明顯了,驗(yàn)證證書時(shí),無法驗(yàn)證。

上面提供的解決方案就是添加默認(rèn)信任全部證書。以此來通過接下來的通信。

但是,這樣問題是解決了。但是覺得還是不帶靠譜(信任全部證書有點(diǎn)危險(xiǎn))。繼續(xù)噼噼啪啪的網(wǎng)上搜索一番。又找到了一種解決方案,其過程大致這樣的:

1.瀏覽器訪問https地址,保存提示的證書到本地,放到android項(xiàng)目中的assets目錄。

2.導(dǎo)入證書,代碼如下。

3.把證書添加為信任。

  1. public static String requestHTTPSPage(Context context, String mUrl) { 
  2.         InputStream ins = null
  3.         String result = ""
  4.         try { 
  5.             ins = context.getAssets().open("my.key"); // 下載的證書放到項(xiàng)目中的assets目錄中 
  6.             CertificateFactory cerFactory = CertificateFactory.getInstance("X.509"); 
  7.             Certificate cer = cerFactory.generateCertificate(ins); 
  8.             KeyStore keyStore = KeyStore.getInstance("PKCS12""BC"); 
  9.             keyStore.load(nullnull); 
  10.             keyStore.setCertificateEntry("trust", cer); 
  11.   
  12.             SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore); 
  13.             Scheme sch = new Scheme("https", socketFactory, 443); 
  14.             HttpClient mHttpClient = new DefaultHttpClient(); 
  15.             mHttpClient.getConnectionManager().getSchemeRegistry().register(sch); 
  16.   
  17.             BufferedReader reader = null
  18.             try { 
  19.                 HttpGet request = new HttpGet(); 
  20.                 request.setURI(new URI(mUrl)); 
  21.                 HttpResponse response = mHttpClient.execute(request); 
  22.                 if (response.getStatusLine().getStatusCode() != 200) { 
  23.                     request.abort(); 
  24.                     return result; 
  25.                 } 
  26.   
  27.                 reader = new BufferedReader(new InputStreamReader(response 
  28.                         .getEntity().getContent())); 
  29.                 StringBuffer buffer = new StringBuffer(); 
  30.                 String line = null
  31.                 while ((line = reader.readLine()) != null) { 
  32.                     buffer.append(line); 
  33.                 } 
  34.                 result = buffer.toString(); 
  35.             } catch (Exception e) { 
  36.                 e.printStackTrace(); 
  37.             } finally { 
  38.                 if (reader != null) { 
  39.                     reader.close(); 
  40.                 } 
  41.             } 
  42.         } catch (Exception e) { 
  43.             e.printStackTrace(); 
  44.         } finally { 
  45.             try { 
  46.                 if (ins != null
  47.                     ins.close(); 
  48.             } catch (IOException e) { 
  49.                 e.printStackTrace(); 
  50.             } 
  51.         } 
  52.         return result; 

本文鏈接:http://my.oschina.net/u/1251149/blog/299010

責(zé)任編輯:chenqingxiang 來源: oschina
相關(guān)推薦

2017-10-23 13:20:37

2010-09-13 12:19:03

2019-04-24 11:41:32

云計(jì)算開發(fā)數(shù)據(jù)中心

2017-06-07 11:37:47

2018-11-30 16:17:28

HTTPS

2021-09-15 08:09:43

前端技術(shù)編程

2018-10-29 15:20:03

2010-05-31 17:56:27

2024-08-02 08:21:52

Spring項(xiàng)目方式

2021-07-14 15:01:14

智能電網(wǎng)技術(shù)物聯(lián)網(wǎng)

2022-02-23 12:35:12

LibreOffic無障礙輔助套件

2010-08-13 08:57:20

Flex主題

2017-05-24 09:43:42

2015-04-20 10:45:43

2010-04-16 16:39:25

Oracle細(xì)粒度

2013-03-26 13:38:12

Android per

2010-03-16 11:07:19

ArrayiPhone應(yīng)用交付

2011-07-25 17:50:42

PostgreSQLODBC

2023-05-10 10:37:41

谷歌ChromeCookie

2011-12-09 20:28:50

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)