欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析Android系統(tǒng)中HTTPS通信的實(shí)現(xiàn)

 更新時(shí)間:2015年07月31日 14:48:04   作者:低調(diào)小一  
這篇文章主要介紹了淺析Android系統(tǒng)中HTTPS通信的實(shí)現(xiàn),實(shí)現(xiàn)握手的源碼為Java語(yǔ)言編寫(xiě),需要的朋友可以參考下

前言
最近有一個(gè)跟HTTPS相關(guān)的問(wèn)題需要解決,因此花時(shí)間學(xué)習(xí)了一下Android平臺(tái)HTTPS的使用,同時(shí)也看了一些HTTPS的原理,這里分享一下學(xué)習(xí)心得。

HTTPS原理
HTTPS(Hyper Text Transfer Protocol Secure),是一種基于SSL/TLS的HTTP,所有的HTTP數(shù)據(jù)都是在SSL/TLS協(xié)議封裝之上進(jìn)行傳輸?shù)?。HTTPS協(xié)議是在HTTP協(xié)議的基礎(chǔ)上,添加了SSL/TLS握手以及數(shù)據(jù)加密傳輸,也屬于應(yīng)用層協(xié)議。所以,研究HTTPS協(xié)議原理,最終就是研究SSL/TLS協(xié)議。
SSL/TLS協(xié)議作用
不使用SSL/TLS的HTTP通信,就是不加密的通信,所有的信息明文傳播,帶來(lái)了三大風(fēng)險(xiǎn):
1. 竊聽(tīng)風(fēng)險(xiǎn):第三方可以獲知通信內(nèi)容。
2. 篡改風(fēng)險(xiǎn):第三方可以修改通知內(nèi)容。
3. 冒充風(fēng)險(xiǎn):第三方可以冒充他人身份參與通信。
SSL/TLS協(xié)議是為了解決這三大風(fēng)險(xiǎn)而設(shè)計(jì)的,希望達(dá)到:
1. 所有信息都是加密傳輸,第三方無(wú)法竊聽(tīng)。
2. 具有校驗(yàn)機(jī)制,一旦被篡改,通信雙方都會(huì)立刻發(fā)現(xiàn)。
3. 配備身份證書(shū),防止身份被冒充。
基本的運(yùn)行過(guò)程
SSL/TLS協(xié)議的基本思路是采用公鑰加密法,也就是說(shuō),客戶(hù)端先向服務(wù)器端索要公鑰,然后用公鑰加密信息,服務(wù)器收到密文后,用自己的私鑰解密。但是這里需要了解兩個(gè)問(wèn)題的解決方案。
1. 如何保證公鑰不被篡改?
解決方法:將公鑰放在數(shù)字證書(shū)中。只要證書(shū)是可信的,公鑰就是可信的。
2. 公鑰加密計(jì)算量太大,如何減少耗用的時(shí)間?
解決方法:每一次對(duì)話(huà)(session),客戶(hù)端和服務(wù)器端都生成一個(gè)“對(duì)話(huà)密鑰”(session key),用它來(lái)加密信息。由于“對(duì)話(huà)密鑰”是對(duì)稱(chēng)加密,所以運(yùn)算速度非???,而服務(wù)器公鑰只用于加密“對(duì)話(huà)密鑰”本身,這樣就減少了加密運(yùn)算的消耗時(shí)間。
因此,SSL/TLS協(xié)議的基本過(guò)程是這樣的:
1. 客戶(hù)端向服務(wù)器端索要并驗(yàn)證公鑰。
2. 雙方協(xié)商生成“對(duì)話(huà)密鑰”。
3. 雙方采用“對(duì)話(huà)密鑰”進(jìn)行加密通信。
上面過(guò)程的前兩布,又稱(chēng)為“握手階段”。
握手階段的詳細(xì)過(guò)程

2015731144521263.png (599×662)

握手階段”涉及四次通信,需要注意的是,“握手階段”的所有通信都是明文的。
客戶(hù)端發(fā)出請(qǐng)求(ClientHello)
首先,客戶(hù)端(通常是瀏覽器)先向服務(wù)器發(fā)出加密通信的請(qǐng)求,這被叫做ClientHello請(qǐng)求。在這一步中,客戶(hù)端主要向服務(wù)器提供以下信息:
1. 支持的協(xié)議版本,比如TLS 1.0版
2. 一個(gè)客戶(hù)端生成的隨機(jī)數(shù),稍后用于生成“對(duì)話(huà)密鑰”。
3. 支持的加密方法,比如RSA公鑰加密。
4. 支持的壓縮方法。
這里需要注意的是,客戶(hù)端發(fā)送的信息之中不包括服務(wù)器的域名。也就是說(shuō),理論上服務(wù)器只能包含一個(gè)網(wǎng)站,否則會(huì)分不清應(yīng)用向客戶(hù)端提供哪一個(gè)網(wǎng)站的數(shù)字證書(shū)。這就是為什么通常一臺(tái)服務(wù)器只能有一張數(shù)字證書(shū)的原因。
服務(wù)器回應(yīng)(ServerHello)
服務(wù)器收到客戶(hù)端請(qǐng)求后,向客戶(hù)端發(fā)出回應(yīng),這叫做ServerHello。服務(wù)器的回應(yīng)包含以下內(nèi)容:
1. 確認(rèn)使用的加密通信協(xié)議版本,比如TLS 1.0版本。如果瀏覽器與服務(wù)器支持的版本不一致,服務(wù)器關(guān)閉加密通信。
2. 一個(gè)服務(wù)器生成的隨機(jī)數(shù),稍后用于生成“對(duì)話(huà)密鑰”。
3. 確認(rèn)使用的加密方法,比如RSA公鑰加密。
4. 服務(wù)器證書(shū)。
除了上面這些信息,如果服務(wù)器需要確認(rèn)客戶(hù)端的身份,就會(huì)再包含一項(xiàng)請(qǐng)求,要求客戶(hù)端提供“客戶(hù)端證書(shū)”。比如,金融機(jī)構(gòu)往往只允許認(rèn)證客戶(hù)連入自己的網(wǎng)絡(luò),就會(huì)向正式客戶(hù)提供USB密鑰,里面就包含了一張客戶(hù)端證書(shū)。
客戶(hù)端回應(yīng)
客戶(hù)端收到服務(wù)器回應(yīng)以后,首先驗(yàn)證服務(wù)器證書(shū)。如果證書(shū)不是可信機(jī)構(gòu)頒發(fā),或者證書(shū)中的域名與實(shí)際域名不一致,或者證書(shū)已經(jīng)過(guò)期,就會(huì)向訪(fǎng)問(wèn)者顯示一個(gè)警告,由其選擇是否還要繼續(xù)通信。
如果證書(shū)沒(méi)有問(wèn)題,客戶(hù)端就會(huì)從證書(shū)中取出服務(wù)器的公鑰。然后,向服務(wù)器發(fā)送下面三項(xiàng)消息。
1. 一個(gè)隨機(jī)數(shù)。該隨機(jī)數(shù)用服務(wù)器公鑰加密,防止被竊聽(tīng)。
2. 編碼改變通知,表示隨后的信息都將用雙方商定的加密方法和密鑰發(fā)送。
3. 客戶(hù)端握手結(jié)束通知,表示客戶(hù)端的握手階段已經(jīng)結(jié)束。這一項(xiàng)通常也是前面發(fā)送的所有內(nèi)容的hash值,用來(lái)供服務(wù)器校驗(yàn)。
上面第一項(xiàng)隨機(jī)數(shù),是整個(gè)握手階段出現(xiàn)的第三個(gè)隨機(jī)數(shù),又稱(chēng)“pre-master key”。有了它以后,客戶(hù)端和服務(wù)器就同時(shí)有了三個(gè)隨機(jī)數(shù),接著雙方就用事先商定的加密方法,各自生成本次會(huì)話(huà)所用的同一把“會(huì)話(huà)密鑰”。
服務(wù)器的最后回應(yīng)
服務(wù)器收到客戶(hù)端的第三個(gè)隨機(jī)數(shù)pre-master key之后,計(jì)算生成本次會(huì)話(huà)所用的“會(huì)話(huà)密鑰”。然后,向客戶(hù)端最后發(fā)送下面信息。
1. 編碼改變通知,表示隨后的信息都將用雙方商定的加密方法和密鑰發(fā)送。
2. 服務(wù)器握手結(jié)束通知,表示服務(wù)器的握手階段已經(jīng)結(jié)束。這一項(xiàng)同時(shí)也是前面發(fā)生的所有內(nèi)容的hash值,用來(lái)供客戶(hù)端校驗(yàn)。
握手結(jié)束
至此,整個(gè)握手階段全部結(jié)束。接下來(lái),客戶(hù)端與服務(wù)器進(jìn)入加密通信,就完全是使用普通的HTTP協(xié)議,只不過(guò)用“會(huì)話(huà)密鑰”加密內(nèi)容。

服務(wù)器基于Nginx搭建HTTPS虛擬站點(diǎn)
之前一篇文章詳細(xì)介紹了在服務(wù)器端如何生成SSL證書(shū),并基于Nginx搭建HTTPS服務(wù)器,鏈接:Nginx搭建HTTPS服務(wù)器

Android實(shí)現(xiàn)HTTPS通信
由于各種原因吧,這里使用HttpClicent類(lèi)講解一下Android如何建立HTTPS連接。代碼demo如下。
MainActivity.java

   

 package com.example.photocrop; 
   
  import java.io.BufferedReader; 
  import java.io.InputStreamReader; 
   
  import org.apache.http.HttpResponse; 
  import org.apache.http.HttpStatus; 
  import org.apache.http.StatusLine; 
  import org.apache.http.client.HttpClient; 
  import org.apache.http.client.methods.HttpPost; 
  import org.apache.http.client.methods.HttpUriRequest; 
   
  import android.app.Activity; 
  import android.os.AsyncTask; 
  import android.os.Bundle; 
  import android.os.AsyncTask.Status; 
  import android.text.TextUtils; 
  import android.util.Log; 
  import android.view.View; 
  import android.widget.Button; 
  import android.widget.TextView; 
   
  public class MainActivity extends Activity { 
    private Button httpsButton; 
    private TextView conTextView; 
   
    private CreateHttpsConnTask httpsTask; 
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
   
      httpsButton = (Button) findViewById(R.id.create_https_button); 
      httpsButton.setOnClickListener(new View.OnClickListener() { 
   
        @Override 
        public void onClick(View v) { 
          runHttpsConnection(); 
        } 
      }); 
   
      conTextView = (TextView) findViewById(R.id.content_textview); 
      conTextView.setText("初始為空"); 
    } 
   
    private void runHttpsConnection() { 
      if (httpsTask == null || httpsTask.getStatus() == Status.FINISHED) { 
        httpsTask = new CreateHttpsConnTask(); 
        httpsTask.execute(); 
      } 
    } 
   
    private class CreateHttpsConnTask extends AsyncTask<Void, Void, Void> { 
      private static final String HTTPS_EXAMPLE_URL = "自定義"; 
      private StringBuffer sBuffer = new StringBuffer(); 
   
      @Override 
      protected Void doInBackground(Void... params) { 
        HttpUriRequest request = new HttpPost(HTTPS_EXAMPLE_URL); 
        HttpClient httpClient = HttpUtils.getHttpsClient(); 
        try { 
          HttpResponse httpResponse = httpClient.execute(request); 
          if (httpResponse != null) { 
            StatusLine statusLine = httpResponse.getStatusLine(); 
            if (statusLine != null 
                && statusLine.getStatusCode() == HttpStatus.SC_OK) { 
              BufferedReader reader = null; 
              try { 
                reader = new BufferedReader(new InputStreamReader( 
                    httpResponse.getEntity().getContent(), 
                    "UTF-8")); 
                String line = null; 
                while ((line = reader.readLine()) != null) { 
                  sBuffer.append(line); 
                } 
   
              } catch (Exception e) { 
                Log.e("https", e.getMessage()); 
              } finally { 
                if (reader != null) { 
                  reader.close(); 
                  reader = null; 
                } 
              } 
            } 
          } 
   
        } catch (Exception e) { 
          Log.e("https", e.getMessage()); 
        } finally { 
   
        } 
   
        return null; 
      } 
   
      @Override 
      protected void onPostExecute(Void result) { 
        if (!TextUtils.isEmpty(sBuffer.toString())) { 
          conTextView.setText(sBuffer.toString()); 
        } 
      } 
   
    } 
  } 

HttpUtils.java

   

package com.example.photocrop; 
   
  import org.apache.http.HttpVersion; 
  import org.apache.http.client.HttpClient; 
  import org.apache.http.conn.ClientConnectionManager; 
  import org.apache.http.conn.scheme.PlainSocketFactory; 
  import org.apache.http.conn.scheme.Scheme; 
  import org.apache.http.conn.scheme.SchemeRegistry; 
  import org.apache.http.conn.ssl.SSLSocketFactory; 
  import org.apache.http.impl.client.DefaultHttpClient; 
  import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; 
  import org.apache.http.params.BasicHttpParams; 
  import org.apache.http.params.HttpProtocolParams; 
  import org.apache.http.protocol.HTTP; 
   
  import android.content.Context; 
   
   
  public class HttpUtils { 
    public static HttpClient getHttpsClient() { 
      BasicHttpParams params = new BasicHttpParams(); 
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
      HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
      HttpProtocolParams.setUseExpectContinue(params, true); 
       
      SchemeRegistry schReg = new SchemeRegistry(); 
      schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
       
      ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg); 
       
      return new DefaultHttpClient(connMgr, params); 
    } 
     
    public static HttpClient getCustomClient() { 
      BasicHttpParams params = new BasicHttpParams(); 
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
      HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
      HttpProtocolParams.setUseExpectContinue(params, true); 
       
      SchemeRegistry schReg = new SchemeRegistry(); 
      schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      schReg.register(new Scheme("https", MySSLSocketFactory.getSocketFactory(), 443)); 
       
      ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg); 
       
      return new DefaultHttpClient(connMgr, params); 
    } 
     
    public static HttpClient getSpecialKeyStoreClient(Context context) { 
      BasicHttpParams params = new BasicHttpParams(); 
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
      HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
      HttpProtocolParams.setUseExpectContinue(params, true); 
       
      SchemeRegistry schReg = new SchemeRegistry(); 
      schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      schReg.register(new Scheme("https", CustomerSocketFactory.getSocketFactory(context), 443)); 
       
      ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg); 
       
      return new DefaultHttpClient(connMgr, params); 
    } 
  } 

activity_main.xml

   

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
   
    <Button 
      android:id="@+id/create_https_button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello_world" 
      android:textSize="16sp" /> 
   
    <TextView 
      android:id="@+id/content_textview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:textSize="16sp" /> 
   
  </LinearLayout> 

Android使用DefaultHttpClient建立HTTPS連接,關(guān)鍵需要加入對(duì)HTTPS的支持:

  schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 

加入對(duì)HTTPS的支持,就可以有效的建立HTTPS連接了,例如“https://www.google.com.hk”了,但是訪(fǎng)問(wèn)自己基于Nginx搭建的HTTPS服務(wù)器卻不行,因?yàn)樗褂昧瞬槐幌到y(tǒng)承認(rèn)的自定義證書(shū),會(huì)報(bào)出如下問(wèn)題:No peer certificate。
使用自定義證書(shū)并忽略驗(yàn)證的HTTPS連接方式
解決證書(shū)不被系統(tǒng)承認(rèn)的方法,就是跳過(guò)系統(tǒng)校驗(yàn)。要跳過(guò)系統(tǒng)校驗(yàn),就不能再使用系統(tǒng)標(biāo)準(zhǔn)的SSL SocketFactory了,需要自定義一個(gè)。然后為了在這個(gè)自定義SSL SocketFactory里跳過(guò)校驗(yàn),還需要自定義一個(gè)TrustManager,在其中忽略所有校驗(yàn),即TrustAll。

   

package com.example.photocrop; 
   
  import java.io.IOException; 
  import java.net.Socket; 
  import java.net.UnknownHostException; 
  import java.security.KeyManagementException; 
  import java.security.KeyStore; 
  import java.security.KeyStoreException; 
  import java.security.NoSuchAlgorithmException; 
  import java.security.UnrecoverableKeyException; 
  import java.security.cert.CertificateException; 
  import java.security.cert.X509Certificate; 
   
  import javax.net.ssl.SSLContext; 
  import javax.net.ssl.TrustManager; 
  import javax.net.ssl.X509TrustManager; 
  import org.apache.http.conn.ssl.SSLSocketFactory; 
   
  public class MySSLSocketFactory extends SSLSocketFactory { 
    SSLContext sslContext = SSLContext.getInstance("TLS"); 
   
    public MySSLSocketFactory(KeyStore truststore) 
        throws NoSuchAlgorithmException, KeyManagementException, 
        KeyStoreException, UnrecoverableKeyException { 
      super(truststore); 
      TrustManager tm = new X509TrustManager() { 
   
        @Override 
        public X509Certificate[] getAcceptedIssuers() { 
          return null; 
        } 
   
        @Override 
        public void checkServerTrusted(X509Certificate[] chain, 
            String authType) throws CertificateException { 
   
        } 
   
        @Override 
        public void checkClientTrusted(X509Certificate[] chain, 
            String authType) throws CertificateException { 
   
        } 
      }; 
   
      sslContext.init(null, new TrustManager[] { tm }, null); 
    } 
   
    @Override 
    public Socket createSocket() throws IOException { 
      return sslContext.getSocketFactory().createSocket(); 
    } 
   
    @Override 
    public Socket createSocket(Socket socket, String host, int port, 
        boolean autoClose) throws IOException, UnknownHostException { 
      return sslContext.getSocketFactory().createSocket(socket, host, port, 
          autoClose); 
    } 
   
    public static SSLSocketFactory getSocketFactory() { 
      try { 
        KeyStore trustStore = KeyStore.getInstance(KeyStore 
            .getDefaultType()); 
        trustStore.load(null, null); 
        SSLSocketFactory factory = new MySSLSocketFactory(trustStore); 
        return factory; 
      } catch (Exception e) { 
        e.getMessage(); 
        return null; 
      } 
    } 
  } 

同時(shí),需要修改DefaultHttpClient的register方法,改為自己構(gòu)建的sslsocket:

 

  public static HttpClient getCustomClient() { 
    BasicHttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpProtocolParams.setUseExpectContinue(params, true); 
     
    SchemeRegistry schReg = new SchemeRegistry(); 
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schReg.register(new Scheme("https", MySSLSocketFactory.getSocketFactory(), 443)); 
     
    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg); 
     
    return new DefaultHttpClient(connMgr, params); 
  } 

這樣就可以成功的訪(fǎng)問(wèn)自己構(gòu)建的基于Nginx的HTTPS虛擬站點(diǎn)了。
缺陷:
不過(guò),雖然這個(gè)方案使用了HTTPS,客戶(hù)端和服務(wù)器端的通信內(nèi)容得到了加密,嗅探程序無(wú)法得到傳輸?shù)膬?nèi)容,但是無(wú)法抵擋“中間人攻擊”。例如,在內(nèi)網(wǎng)配置一個(gè)DNS,把目標(biāo)服務(wù)器域名解析到本地的一個(gè)地址,然后在這個(gè)地址上使用一個(gè)中間服務(wù)器作為代理,它使用一個(gè)假的證書(shū)與客戶(hù)端通訊,然后再由這個(gè)代理服務(wù)器作為客戶(hù)端連接到實(shí)際的服務(wù)器,用真的證書(shū)與服務(wù)器通訊。這樣所有的通訊內(nèi)容都會(huì)經(jīng)過(guò)這個(gè)代理,而客戶(hù)端不會(huì)感知,這是由于客戶(hù)端不校驗(yàn)服務(wù)器公鑰證書(shū)導(dǎo)致的。

使用自定義證書(shū)建立HTTPS連接
為了防止上面方案可能導(dǎo)致的“中間人攻擊”,我們可以下載服務(wù)器端公鑰證書(shū),然后將公鑰證書(shū)編譯到Android應(yīng)用中,由應(yīng)用自己來(lái)驗(yàn)證證書(shū)。
生成KeyStore

要驗(yàn)證自定義證書(shū),首先要把證書(shū)編譯到應(yīng)用中,這需要使用keytool工具生產(chǎn)KeyStore文件。這里的證書(shū)就是指目標(biāo)服務(wù)器的公鑰,可以從web服務(wù)器配置的.crt文件或.pem文件獲得。同時(shí),你需要配置bouncycastle,我下載的是bcprov-jdk16-145.jar,至于配置大家自行g(shù)oogle就好了。

  keytool -importcert -v -trustcacerts -alias example -file www.example.com.crt -keystore example.bks -storetype BKS -providerclass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath /home/wzy/Downloads/java/jdk1.7.0_60/jre/lib/ext/bcprov-jdk16-145.jar -storepass pw123456 

運(yùn)行后將顯示證書(shū)內(nèi)容并提示你是否確認(rèn),輸入Y回車(chē)即可。

生產(chǎn)KeyStore文件成功后,將其放在app應(yīng)用的res/raw目錄下即可。
使用自定義KeyStore實(shí)現(xiàn)連接
思路和TrushAll差不多,也是需要一個(gè)自定義的SSLSokcetFactory,不過(guò)因?yàn)檫€需要驗(yàn)證證書(shū),因此不需要自定義TrustManager了。

   

 package com.example.photocrop; 
   
  import java.io.IOException; 
  import java.io.InputStream; 
  import java.security.KeyManagementException; 
  import java.security.KeyStore; 
  import java.security.KeyStoreException; 
  import java.security.NoSuchAlgorithmException; 
  import java.security.UnrecoverableKeyException; 
   
  import org.apache.http.conn.ssl.SSLSocketFactory; 
   
  import android.content.Context; 
   
  public class CustomerSocketFactory extends SSLSocketFactory { 
   
    private static final String PASSWD = "pw123456"; 
   
    public CustomerSocketFactory(KeyStore truststore) 
        throws NoSuchAlgorithmException, KeyManagementException, 
        KeyStoreException, UnrecoverableKeyException { 
      super(truststore); 
    } 
   
    public static SSLSocketFactory getSocketFactory(Context context) { 
      InputStream input = null; 
      try { 
        input = context.getResources().openRawResource(R.raw.example); 
        KeyStore trustStore = KeyStore.getInstance(KeyStore 
            .getDefaultType()); 
   
        trustStore.load(input, PASSWD.toCharArray()); 
   
        SSLSocketFactory factory = new CustomerSocketFactory(trustStore); 
   
        return factory; 
      } catch (Exception e) { 
        e.printStackTrace(); 
        return null; 
      } finally { 
        if (input != null) { 
          try { 
            input.close(); 
          } catch (IOException e) { 
            e.printStackTrace(); 
          } 
          input = null; 
        } 
      } 
    } 
   
  } 

同時(shí),需要修改DefaultHttpClient的register方法,改為自己構(gòu)建的sslsocket:

 

  public static HttpClient getSpecialKeyStoreClient(Context context) { 
    BasicHttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpProtocolParams.setUseExpectContinue(params, true); 
     
    SchemeRegistry schReg = new SchemeRegistry(); 
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schReg.register(new Scheme("https", CustomerSocketFactory.getSocketFactory(context), 443)); 
     
    ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg); 
     
    return new DefaultHttpClient(connMgr, params); 
  } 

相關(guān)文章

最新評(píng)論