androd中使用 https ssl
来源:互联网 发布:阿沁的淘宝店是正品吗 编辑:程序博客网 时间:2024/06/07 14:33
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(); } } }如果有什么地方不妥的地方,请指正,不胜感激。
- androd中使用 https ssl
- TOMCAT使用SSL(https)
- 在IIS中使用SSL配置HTTPS网站
- Andorid 中使用Https/SSL Apache HttpClient(DefaultHttpClient)
- 在 ASP.NET MVC 中使用 HTTPS (SSL/TLS)
- 在 ASP.NET MVC 中使用 HTTPS (SSL/TLS)
- Android中使用https(HTTP+SSL)访问服务器
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
- Springboot配置使用ssl,使用https
- AFNetworking 中 设置 https的SSL认证
- AFNetworking 中 设置 https的SSL认证
- https中SSL协议-openssl实现
- squid添加https协议中ssl设置
- nginx使用ssl模块配置HTTPS支持
- paip.提升安全性-----使用HTTPS SSL
- nginx使用ssl模块配置HTTPS支持
- nginx使用ssl模块配置HTTPS支持
- 继承的访问权限
- 专题——线段树和树状数组(HDOJ)09 Turing Tree 【hdu 3333】
- 制作Linux映像和Kernel的启动
- iOS: Objective C "Selector" 用法
- 云计算加密
- androd中使用 https ssl
- 云计算加密
- [DOS]BAT常用知识
- GitHub 与社会化编程
- 云计算加密
- 加密学习地址
- Qt Command Prompt命令目录
- ibatis学习2
- MySQL 管理