https遇到自签名证书/信任证书


对于CA机构颁发的证书Okhttp默认支持 可以直接访问
但是对于自定义的证书就不可以了(如:https ://kyfw.12306.cn/otn/), 需要加入Trust
下面分两部分来写,一是信任所有证书,二是信任指定证书,访问自签名的网站

一、信任所有证书

1. 在你的module 下 build.gradule里添加

1
2
3
4
dependencies {
、、、
compile 'com.zhy:okhttputils:2.6.2'
、、、}

2.新建MyApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class MyApplication extends Application {
     @Override
     public void onCreate() {
         super.onCreate();
         // 这就是信任所有证书
         HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory( null , null , null );
         OkHttpClient okHttpClient = new OkHttpClient.Builder()
                     .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                     .readTimeout(10000L, TimeUnit.MILLISECONDS)
                     .addInterceptor( new LoggerInterceptor( "TAG" ))
                     .hostnameVerifier( new HostnameVerifier() {
                         @Override
                         public boolean verify(String hostname, SSLSession session) {
                             return true ;
                         }
                     })
                     .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                     .build();
             OkHttpUtils.initClient(okHttpClient);
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }

3.AndroidMainfest.xml

1
2
3
4
5
6
7
8
//添加权限
  "android.permission.INTERNET" >
     "android.permission.WRITE_EXTERNAL_STORAGE" >
 
    
         //将新建的MyApplication name添加进来
         android:name= ".MyApplication"
         .....

4.然后在需要请求网络的地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void getHttps(View view) {
         String url = "https://kyfw.12306.cn/otn/" ;
 
         OkHttpUtils
                 . get ()
                 .url(url)
                 .build()
                 .execute( new StringCallback() {
                     @Override
                     public void onError(Call call, Exception e, int id) {
                         Log.e( "TAG" , "onError: " + e.toString());
                     }
 
                     @Override
                     public void onResponse(String response, int id) {
                         Log.e( "TAG" , "onResponse: " + response.toString());
                     }
                 });
     }

二、自定义的证书 自签名网站

分两种:1.直接使用网站证书;2.将证书转为字符串

1、直接使用网站证书####

1.导出网站证书
-- chrome浏览器方法:(其他浏览器请百度)
--访问 https://kyfw.12306.cn/otn/
按F12
--

 

-- 详细信息--复制到文件--下一步
-- 选
 
 
--下一步
--选择保存位置 填入文件名
 

--下一步 --完成--确定
2、将导出的证书 放在assets文件夹下,没有就自己创建一个
 

1
2
//使用  在Application里
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory( new InputStream[]{getAssets().open( "srca.cer" )}, null , null );

==========
也可以不用证书 直接将证书转为字符串 ,使用字符串

2、将证书转为字符串####

两种方式任选一种 直接使用证书 就不用这一步了
代码 http://download.csdn.net/download/eryhytkmgfhnghnjfghn/9834086

1.在你的module 下 build.gradule里添加

1
2
3
4
dependencies {
、、、
compile 'com.zhy:okhttputils:2.6.2'
、、、}

2.新建MyApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class MyApplication extends Application {
           //获取12306网站证书,将证书中的内容提取出来,写成字符串常量
//不会提取内容 请百度
//我的在asli是可以直接看到内容的
             private String CER_12306 = "-----BEGIN CERTIFICATE-----\n" +
             "MIICmjCCAgOgAwIBAgIIbyZr5/jKH6QwDQYJKoZIhvcNAQEFBQAwRzELMAkGA1UEBhMCQ04xKTAn\n" +
             "BgNVBAoTIFNpbm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMB4X\n" +
             "DTA5MDUyNTA2NTYwMFoXDTI5MDUyMDA2NTYwMFowRzELMAkGA1UEBhMCQ04xKTAnBgNVBAoTIFNp\n" +
             "bm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMIGfMA0GCSqGSIb3\n" +
             "DQEBAQUAA4GNADCBiQKBgQDMpbNeb34p0GvLkZ6t72/OOba4mX2K/eZRWFfnuk8e5jKDH+9BgCb2\n" +
             "9bSotqPqTbxXWPxIOz8EjyUO3bfR5pQ8ovNTOlks2rS5BdMhoi4sUjCKi5ELiqtyww/XgY5iFqv6\n" +
             "D4Pw9QvOUcdRVSbPWo1DwMmH75It6pk/rARIFHEjWwIDAQABo4GOMIGLMB8GA1UdIwQYMBaAFHle\n" +
             "tne34lKDQ+3HUYhMY4UsAENYMAwGA1UdEwQFMAMBAf8wLgYDVR0fBCcwJTAjoCGgH4YdaHR0cDov\n" +
             "LzE5Mi4xNjguOS4xNDkvY3JsMS5jcmwwCwYDVR0PBAQDAgH+MB0GA1UdDgQWBBR5XrZ3t+JSg0Pt\n" +
             "x1GITGOFLABDWDANBgkqhkiG9w0BAQUFAAOBgQDGrAm2U/of1LbOnG2bnnQtgcVaBXiVJF8LKPaV\n" +
             "23XQ96HU8xfgSZMJS6U00WHAI7zp0q208RSUft9wDq9ee///VOhzR6Tebg9QfyPSohkBrhXQenvQ\n" +
             "og555S+C3eJAAVeNCTeMS3N/M5hzBRJAoffn3qoYdAO1Q8bTguOi+2849A==\n" +
             "-----END CERTIFICATE-----" ;
     @Override
     public void onCreate() {
         super.onCreate();
         // 这就是信任所有证书   **和 一 的区别在这里**
         //这里  import okio.Buffer;  
         HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory( new InputStream[]{ new Buffer().writeUtf8(CER_12306).inputStream()}, null , null );
         OkHttpClient okHttpClient = new OkHttpClient.Builder()
                     .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                     .readTimeout(10000L, TimeUnit.MILLISECONDS)
                     .addInterceptor( new LoggerInterceptor( "TAG" ))
                     .hostnameVerifier( new HostnameVerifier() {
                         @Override
                         public boolean verify(String hostname, SSLSession session) {
                             return true ;
                         }
                     })
                     .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                     .build();
             OkHttpUtils.initClient(okHttpClient);
         } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }

然后同一的34

 

代码 https://github.com/DeadLine837/TestHttps

參考 https://github.com/hongyangAndroid/okhttputils

你可能感兴趣的:(https遇到自签名证书/信任证书)