HttpClient和OkHttp关于Https请求问题
来源:互联网 发布:中国物联网域名注册 编辑:程序博客网 时间:2024/06/05 16:20
项目中遇到登录接口,使用的时https接口,所以客户端必须要进行配置,原先的项目使用的时Apache的Httpclient,Android的api接口也内置了httpclient,但是Android系统在4.x以后不建议使用了Httpclient,推荐使用urlconnection,但是urlconnection毕竟太过于原始,所以我选择了okhttp作为http请求库。
OKhttp的地址
由于服务器使用的是通用的https加密,所以我在这里只是介绍通用的加密方式。
Httpclient
static class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; sslContext.init(null, new TrustManager[]{tm}, null); } @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); } @Override public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } } static HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactoryEx sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } }
OkHttp
static class HttpStackPlus extends HurlStack { private final OkUrlFactory mFactory; public HttpStackPlus() { this(new OkHttpClient()); } public HttpStackPlus(OkHttpClient client) { if (client == null) { throw new NullPointerException("Client must not be null."); } else { try { TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; SSLContext e = SSLContext.getInstance("TLS"); e.init(null, new TrustManager[]{tm}, null); client.setSslSocketFactory(e.getSocketFactory()); } catch (Exception e) { throw new AssertionError(); } this.mFactory = new OkUrlFactory(client); } } protected HttpURLConnection createConnection(URL url) throws IOException { return this.mFactory.open(url); } } public static RequestQueue newRequestQueue(Context context) { return Volley.newRequestQueue(context, new HttpStackPlus()); }
0 0
- HttpClient和OkHttp关于Https请求问题
- OkHttp关于https请求的设置
- OkHttp关于https请求的设置
- jdk7下httpclient请求https问题
- okhttp关于https的证书信任问题
- HttpClient请求Https协议
- HttpClient请求Https协议
- HttpClient请求Https协议
- HTTPClient 发送HTTPS请求
- java-httpclient-HTTPS请求
- HttpClient请求https
- httpClient发送https请求
- httpclient 实现https请求
- Android-OKhttp解决https安全链接请求问题
- 关于OkHttp缓存post请求的问题
- 关于http和https允许请求设置header问题
- okHttp框架的介绍 和关于https的自定义签名证书的问题
- 关于httpclient 请求https (如何绕过证书验证)
- 经典排序算法-------插入排序法
- Dart更近一步,Sky会一统江湖吗?
- 物与物的互通:解析Thread、AllJoyn、OIC、Bonjour的特点
- Bluemix云平台实战日志:IBM Mobile Data云服务和Android应用开发
- Justice is Given by Light,一条计算几何
- HttpClient和OkHttp关于Https请求问题
- poj 3704 Sudoku(Dancing Links)
- NSOperaion相关
- 最佳实战:用Cocos2d-x3.x和C++11编写2048游戏以及游戏AI(全民2048 Android版上线啦)
- 写博文不仅仅是记录,更是思考、总结和提高
- GRASP:Greedy Randomized Adaptive Search Procedures 贪心随机自适应搜索算法的基本思想
- Race to 1 Again [概率]
- jQuery源码学习(版本1.11)-ready
- itext 创建pdf