Android平台访问https 双向认证(下)
来源:互联网 发布:交换机镜像端口抓包 编辑:程序博客网 时间:2024/06/07 01:31
访问https双向认证两种方式
public static void httpsRequest(Context c) {try {String path = "https://localhost:8443/123.html";BasicHttpParams params = new BasicHttpParams();HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT_CONTENT_CHARSET);HttpProtocolParams.setUseExpectContinue(params, true);SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());SchemeRegistry schReg = new SchemeRegistry();schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));// 出错:因为使用了不被系统承认的自定义证书:No peer certificate 。// schReg.register(new Scheme("https",SSLSocketFactory.getSocketFactory(), 443));schReg.register(new Scheme("https", SSLTrustAllSocketFactory .getSocketFactory(c), 443));ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);DefaultHttpClient client = new DefaultHttpClient(connMgr, params);HttpGet request = new HttpGet(path);HttpResponse httpResponse = client.execute(request);int responseCode = httpResponse.getStatusLine().getStatusCode();String message = httpResponse.getStatusLine().getReasonPhrase();HttpEntity entity = httpResponse.getEntity();if (responseCode == 200 && entity != null) {Log.e("log", entity.toString());}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static class SSLTrustAllSocketFactory extends SSLSocketFactory {private static final String TAG = "SSLTrustAllSocketFactory";private SSLContext mCtx; public SSLTrustAllSocketFactory(KeyStore truststore,Context context) throws Throwable {super(truststore);try {// Client should authenticate itself with the valid certificate to Server. InputStream clientStream = context.getResources().openRawResource(R.raw.client); char[] password = "123456".toCharArray();KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(clientStream, password); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, password); // Client should also add the CA certificate obtained from server and create TrustManager from it for the client to validate the // identity of the server. KeyStore trustStore = KeyStore.getInstance("BKS"); InputStream instream = null; instream = context.getResources().openRawResource(R.raw.newserver);// trustStore.setCertificateEntry("dd", certificateFactory.generateCertificate(instream)); try { trustStore.load(instream, "123456".toCharArray()); } catch (Exception e) { e.printStackTrace(); } finally { try { instream.close(); } catch (Exception ignore) {} } String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(trustStore); mCtx = SSLContext.getInstance("TLS"); mCtx.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);// setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);} catch (Exception ex) {}}@Overridepublic Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException {return mCtx.getSocketFactory().createSocket(socket, host, port,autoClose);}@Overridepublic Socket createSocket() throws IOException {return mCtx.getSocketFactory().createSocket();}public static SSLSocketFactory getSocketFactory(Context c) {try {SSLSocketFactory factory = new SSLTrustAllSocketFactory(null,c);return factory;} catch (Throwable e) {Log.d(TAG, e.getMessage());e.printStackTrace();}return null;}}
<pre name="code" class="java">public static void https2(Context context) {try {String path = "https://192.168.0.102:8443/123.html";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));InputStream clientStream = context.getResources().openRawResource(R.raw.client);char[] password = "123456".toCharArray();KeyStore keyStore = KeyStore.getInstance("PKCS12");keyStore.load(clientStream, password);KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());keyManagerFactory.init(keyStore, password);KeyStore trustStore = KeyStore.getInstance("BKS");InputStream instream = null;instream = context.getResources().openRawResource(R.raw.newserver);// trustStore.setCertificateEntry("dd",// certificateFactory.generateCertificate(instream));try {trustStore.load(instream, "123456".toCharArray());} catch (Exception e) {e.printStackTrace();} finally {try {instream.close();} catch (Exception ignore) {}}String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);tmf.init(trustStore);SSLSocketFactory myssl = new SSLSocketFactory(keyStore, "123456",trustStore);myssl.setHostnameVerifier((new AllowAllHostnameVerifier()));schReg.register(new Scheme("https", myssl, 443));ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);DefaultHttpClient client = new DefaultHttpClient(connMgr, params);HttpGet request = new HttpGet(path);HttpResponse httpResponse = client.execute(request);int responseCode = httpResponse.getStatusLine().getStatusCode();String message = httpResponse.getStatusLine().getReasonPhrase();HttpEntity entity = httpResponse.getEntity();if (responseCode == 200 && entity != null) {Log.e("log", entity.toString() + "dddddddddd");}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (KeyManagementException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnrecoverableKeyException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (KeyStoreException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (CertificateException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}
0 0
- Android平台访问https 双向认证(下)
- Android平台访问https 双向认证(上)
- Android访问Https双向认证API
- android SSL证书认证、SSL双向认证、https访问;
- Android Https 双向认证
- Android Https 双向认证
- Keytools Https双向认证(Android通用)
- IOS Android Tomcat SSL双向认证HTTPS访问
- IOS Android Tomcat SSL双向认证HTTPS访问
- https双向认证(基于程序访问,j2ee和android上皆可用)
- https双向认证(基于程序访问,j2ee和android上皆可用)
- android 开启Https双向认证
- Android webview在https下实现ssl的双向认证
- Android webview在https下实现ssl的双向认证
- Android Retrofit HTTPS 单向认证 双向认证
- Apache下配置https双向认证
- tomcat搭建android https双向认证
- 基于java的https双向认证(android)
- UITableView 的操作
- android4.4系统源码编译(四)_问题总结
- hdu 1852(积性函数)
- int (*a)[10] 和 int *a[10]
- REST--深入浅出REST
- Android平台访问https 双向认证(下)
- U盘装系统进不去pe界面
- 对Handler,Looper,messagequeue,HandlerThread的深入理解,Handler常见面试题
- ViewState的用法
- 如何在一个Library下批量Check in Documents
- shell教程
- LISK节点在Linux和Windows下的安装教程(By Phinx
- Q18:树的子结构
- Flask学习-设备管理系统1:数据库修改,表单修改