对于CA机构颁发的证书Okhttp默认支持 可以直接访问
但是对于自定义的证书就不可以了(如:https :), 需要加入Trust 下面分两部分来写,一是信任所有证书,二是信任指定证书,访问自签名的网站一、信任所有证书
1. 在你的module 下 build.gradule里添加
dependencies {、、、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
//添加权限
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浏览器方法:(其他浏览器请百度) --访问 按F12 --//使用 在Application里 HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{getAssets().open("srca.cer")}, null, null);
==========
也可以不用证书 直接将证书转为字符串 ,使用字符串2、将证书转为字符串####
两种方式任选一种 直接使用证书 就不用这一步了
代码1.在你的module 下 build.gradule里添加
dependencies {、、、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
代码
參考