关闭

androd中使用 https ssl

1043人阅读 评论(0) 收藏 举报

android平台上支持的keystore type好像只有PKCS12,不支持JKS。

使用https的前提是,服务器必须首先配置好ssl,配置安全连接各种服务器都有说明文档,在此不多说了、


还必须把服务器证书导出来,通过浏览器就可以导出来,此处导出的证书文件为: my.cer

可以把证书放在asset文件夹下,或者放到raw文件加下,以流的形式读出来。


下面是在android上的代码,经过测试,没有任何问题

public class HttpsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
        	
			https();
			
		} catch (KeyManagementException e) {
			Log.e("https", e.getMessage());
		} catch (ClientProtocolException e) {
			Log.e("https", e.getMessage());
		} catch (KeyStoreException e) {
			Log.e("https", e.getMessage());
		} catch (NoSuchAlgorithmException e) {
			Log.e("https", e.getMessage());
		} catch (CertificateException e) {
			Log.e("https", e.getMessage());
		} catch (UnrecoverableKeyException e) {
			Log.e("https", e.getMessage());
		} catch (IOException e) {
			Log.e("https", e.getMessage());
		}
    }
    public void https() throws ClientProtocolException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException{
    	DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            KeyStore trustStore  = KeyStore.getInstance("PKCS12");
            InputStream instream = getResources().openRawResource(R.raw.my); 
            CertificateFactory cerFactory = CertificateFactory.getInstance("X.509");  
            java.security.cert.Certificate cer = cerFactory.generateCertificate(instream);
            try {
            	trustStore.load(null, null);
            	trustStore.setCertificateEntry("trust", cer);
            } finally {
                try { instream.close(); } catch (Exception ignore) {}
            }
            
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
            Scheme https = new Scheme("https", socketFactory, 8443);
            httpclient.getConnectionManager().getSchemeRegistry().register(https);

            HttpGet httpget = new HttpGet("https://服务器IP:8443/httpsServer/httpstest?username=mmmm&password=pppp");

            System.out.println("executing request" + httpget.getRequestLine());
            Log.i("https", "executing request" + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            Log.i("https", "----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                Log.i("https", "Response content length: " + entity.getContentLength());
            }

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
    
    
        
}
如果有什么地方不妥的地方,请指正,不胜感激。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:748952次
    • 积分:7567
    • 等级:
    • 排名:第2943名
    • 原创:137篇
    • 转载:36篇
    • 译文:3篇
    • 评论:117条
    最新评论