android中进行https连接的方式

网上找到一个英文的示例试了下,发现可以连,就做了个demo放上来原文地址:http://www.anddev.org/viewtopic.php?p=26514

  1. public class Demo extends Activity {
  2.     /** Called when the activity is first created. */
  3.         private TextView text;
  4.     @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         text = (TextView)findViewById(R.id.text);
  9.         GetHttps();
  10.     }
  11.     
  12.     private void GetHttps(){
  13.             String https = " https://800wen.com/";
  14.             try{
  15.                     SSLContext sc = SSLContext.getInstance("TLS");
  16.                     sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
  17.                     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  18.                     HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
  19.                     HttpsURLConnection conn = (HttpsURLConnection)new URL(https).openConnection();
  20.                     conn.setDoOutput(true);
  21.                     conn.setDoInput(true);
  22.                     conn.connect();
  23.                     
  24.                      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
  25.              StringBuffer sb = new StringBuffer(); 
  26.              String line; 
  27.              while ((line = br.readLine()) != null) 
  28.                      sb.append(line); 
  29.                     
  30.                     text.setText(sb.toString());
  31.                     
  32.             }catch(Exception e){
  33.                     Log.e(this.getClass().getName(), e.getMessage());
  34.             }
  35.             
  36.     }
  37.     
  38.     private class MyHostnameVerifier implements HostnameVerifier{

  39.                 @Override
  40.                 public boolean verify(String hostname, SSLSession session) {
  41.                         // TODO Auto-generated method stub
  42.                         return true;
  43.                 }
  44.     }
  45.     
  46.     private class MyTrustManager implements X509TrustManager{

  47.                 @Override
  48.                 public void checkClientTrusted(X509Certificate[] chain, String authType)
  49.                                 throws CertificateException {
  50.                         // TODO Auto-generated method stub
  51.                         
  52.                 }

  53.                 @Override
  54.                 public void checkServerTrusted(X509Certificate[] chain, String authType)
  55.                                 throws CertificateException {
  56.                         // TODO Auto-generated method stub
  57.                         
  58.                 }

  59.                 @Override
  60.                 public X509Certificate[] getAcceptedIssuers() {
  61.                         // TODO Auto-generated method stub
  62.                         return null;
  63.                 }        
  64.     }  
  65. }


你可能感兴趣的:(http协议)