继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

慕森王
关注TA
已关注
手记 428
粉丝 107
获赞 552

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

一、信任所有证书

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

?

1234dependencies {、、、compile 'com.zhy:okhttputils:2.6.2'、、、}

2.新建MyApplication

?

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

?


//添加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>     <application        //将新建的MyApplication name添加进来        android:name=".MyApplication"        .....

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

?


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文件夹下,没有就自己创建一个

 


?

12//使用  在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里添加

?

1234dependencies {、、、compile 'com.zhy:okhttputils:2.6.2'、、、}

2.新建MyApplication

?


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

原文出处

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP