Retrofit2对https请求的实现(干货)
来源:互联网 发布:美国为什么强大知乎 编辑:程序博客网 时间:2024/06/06 05:48
由于项目上传到GooglePlay时被提醒传输数据方式不安全,故改用https加密传输。这里我的项目里用到Retrofit2.2.0,但Retrofit本身的okhttp不能直接请求证书不安全的https,所以得采取一些应急措施。
首先我们在androidstudio里的gradle依赖Retrofit,如下:
compile 'com.squareup.retrofit2:retrofit:2.2.0'compile 'com.squareup.retrofit2:converter-gson:2.2.0'
然后把生成的证书文件key放入项目目录的raw文件夹下,没有raw的话就新建一个即可,这里key是bks后缀的,不是的话自行google如何转换成bks。
这里我们需添加一个新类,为取得SSL一些实例:
public class SslContextFactory { private static final String CLIENT_TRUST_PASSWORD = "*******";//信任证书密码 private static final String CLIENT_AGREEMENT = "TLS";//使用协议 private static final String CLIENT_TRUST_MANAGER = "X509"; private static final String CLIENT_TRUST_KEYSTORE = "BKS"; SSLContext sslContext = null; public SSLContext getSslSocket(Context context) { try {//取得SSL的SSLContext实例 sslContext = SSLContext.getInstance(CLIENT_AGREEMENT);//取得TrustManagerFactory的X509密钥管理器实例 TrustManagerFactory trustManager = TrustManagerFactory.getInstance(CLIENT_TRUST_MANAGER);//取得BKS密库实例 KeyStore tks = KeyStore.getInstance(CLIENT_TRUST_KEYSTORE); InputStream is = context.getResources().openRawResource(R.raw.key); try { tks.load(is, CLIENT_TRUST_PASSWORD.toCharArray()); } finally { is.close(); }//初始化密钥管理器 trustManager.init(tks);//初始化SSLContext sslContext.init(null, trustManager.getTrustManagers(), null); } catch (Exception e) { Log.e("SslContextFactory", e.getMessage()); } return sslContext; }}
//retrofit2访问https SSLSocketFactory sslSocketFactory = new SslContextFactory().getSslSocket(context).getSocketFactory(); OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://test.pengfff/") //url自行配置 .client(okHttpClient.build()) .build(); PostService postService = retrofit.create(PostService.class); Call<ResponseBody> call = postService.postFormUrlEncoded(name,pwd); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { String result = response.body().toString(); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(mcontext, t.toString(), Toast.LENGTH_SHORT).show(); } }); } public interface PostService { @POST("test") @FormUrlEncoded Call<ResponseBody> postFormUrlEncoded(@Field("name") String name, @Field("pwd") String pwd); }
这是一个很普通的POST表单请求,请求方式为HTTPS
最后测试返回结果成功,大家有问题可以评论下面提问,谢谢。
2 0
- Retrofit2对https请求的实现(干货)
- Retrofit2.0 简单实现的网络请求
- RxJava+retrofit2实现网络请求
- 简单实现RxJava2+Okhttp+Retrofit2的网络请求框架封装
- 针对xcode7对https请求的问题。
- Android基于Retrofit2.0和RXJava2.0 的HTTPS网络请求搭建
- Retrofit2的get请求和post请求
- Retrofit2的get请求和post请求
- RxJava+Retrofit2+MVP实现网络请求
- RxJava2和Retrofit2实现简单网络请求
- Android 实现Retrofit2.0请求网络
- IOS UIWebView请求https站点及html页面内部ajax请求 对https的支持
- java 实现https请求
- java 实现https请求
- java 实现https请求
- java 实现https请求
- java 实现https请求
- java 实现https请求
- Linux命令行与shell脚本(10)--获得用户输入
- VUEX兼容IE上的报错问题
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- form表单提交前做方法
- 51nod 2级算法题-1010
- Retrofit2对https请求的实现(干货)
- Python yield用法
- CSUOJ 1898 复盘拉火车(栈+队列模拟)
- 文章标题
- Java中break, continue, return语句的使用区别
- 单链表的基本操作
- OpenCV学习——永久性配置opencv
- Linux操作系统的进程组、会话
- 【SHOI&SXOI2017】bzoj4870 组合数问题