Android中包含了Apache Jakarta Common 下的子项目 HttpClient 类包的一个子集。因此,在大多数情况下可以按照JVM的方式使用HttpClient,但是在有些情况下,由于Android SDK中未包含某些类,处理的方式就有差别。比如,访问https时安全证书的处理。
假如我们的需求是自动接受所有安全证书。思路跟以前一样,扩展一个SSLSocketFactory类,并将TrustManager里的方法全部重写成空。
为了方便使用,我直接在Android的org.apache.http.conn.ssl.SSLSocketFactory源码上扩展。
主要修改了其默认构造方法,如下:
private CustomSSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
super();
// this.sslcontext = null;
// this.socketfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
// this.nameResolver = null;
this.sslcontext = SSLContext.getInstance(TLS);
this.sslcontext.init(null, new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
}, new SecureRandom());
this.socketfactory = this.sslcontext.getSocketFactory();
this.nameResolver = null;
}
定义好SSLSocketFactory之后,再使用它来配置HttpClient并访问Https就和以前一样了。
转载请注明
分享到:
相关推荐
android HttpClient访问某些Https时,出现了问题,无法访问,好像是要安全验证。此Demo解决了此问题,HttpClient能够Https和Http类型的URL了。 在eclipse下打开工程若有乱码,请把eclipse的字符编码改成UTF-8。
使用HttpClient4.5实现https请求忽略SSL证书验证工具类
使用httpclient和httpsurlconnection两种方法访问https网址,同时分为验证证书和不验证证书两种方法
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
Android Studio使用HttpClient请求数据(get请求)不包括post请求
本实例描述了Android平台下使用Apache开源项目的HttpClient访问网络的示例。分别使用HttpClient下的两个子实现类,DefaultHttpClient和AndroidHttpClient分别进行演示。
Android 使用HttpClient代理
Android-异步HttpClient框架,采用两种提交方式 Android-异步HttpClient框架(get和post) Android-异步HttpClient框架 csdn博客地址:http://blog.csdn.net/u014657752
JAVA利用HttpClient进行HTTPS接口调用
NULL 博文链接:https://liudeh-009.iteye.com/blog/1181571
Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...
httpClient自动接受https请求的所有证书代码,修改相应测试url,主函数运行即可
是android的jar中httpclient的源码,是org.apache.http包的源码。其中包含org.apache.http.HttpResponse类。 不是官网上下载的,官网上的源码里面没有org.apache.http.HttpResponse这个类。
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
Android通过HttpClient访问服务器的servlet
java 使用httpclient访问servlet,方便两个不同服务器上的程序相互调用.
NULL 博文链接:https://from-null.iteye.com/blog/859795
httpclient-android Build script and dependencies to create repackaged upstream version of HttpClient and depdendencies (HttpMime, HttpCore, HttpClient-Cache) and get it working on Android API version...
使用方法见:...1. Android使用HTTPCLIENT访问国密SSL协议的HTTPS服务 2. 示例代码为单向认证,可支持双向认证 3. 获取服务端的国密数字证书 4. 适用于Android 7.0(API 24)及以上
Android HttpClient基本使用方法 测试过能用。