本文主要在 okhttp框架基础上进行操作;
首先集成gradle:
代码语言:javascript复制 compile 'com.zhy:okhttputils:2.6.2'
eclipse 哥们请自行百度用法。
主要是的okhttp 框架, 建议没基础的哥们去看看使用方法:
读取证书并转化为流的方法
代码语言:javascript复制//获取指定证书的流对象。
public InputStream[] getAssignInputStreams(String cer){
InputStream ins[] = null;
String result = "";
try {
ins[0] = context.getAssets().open(cer); //下载的证书放到项目中的assets目录中
} catch (IOException e) {
e.printStackTrace();
}
return ins;
}
//获取指定证书的流对象。
public InputStream getAssignInputStream(String cer){
InputStream ins = null;
String result = "";
try {
ins = context.getAssets().open(cer); //下载的证书放到项目中的assets目录中
} catch (IOException e) {
e.printStackTrace();
}
return ins;
}
-设置信任全部证书
代码语言:javascript复制 //设置信任全部证书
public void setAllCertificates(){
HttpsUtils.SSLParams sslParams=HttpsUtils.getSslSocketFactory(null, null, null);
mOkHttpClient.newBuilder().sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
//其他配置
.build();
okHttpUtils.initClient(mOkHttpClient);
}
-设置信任指定证书
代码语言:javascript复制 /**
* 设置信任具体证书
* @param cer 证书的流, 既可以从资产目录里拿也可以设置成String,这里是一个名字。
*/
public void setAssignCertificates(String cer){
HttpsUtils.SSLParams sslParams= HttpsUtils.getSslSocketFactory(getAssignInputStreams(cer), null, null);
mOkHttpClient.newBuilder().sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
//其他配置
.build();
okHttpUtils.initClient(mOkHttpClient);
}
-设置双向验证证书
代码语言:javascript复制 /**
* 设置双向验证
* @param cer 证书的inputstream
* @param Mcer 本地证书的inputstream
* @param psaaword 本地证书的密码
*/
public void setBothwayCertificates(String cer,String Mcer,String psaaword){
HttpsUtils.SSLParams sslParams= HttpsUtils.getSslSocketFactory(getAssignInputStreams(cer), getAssignInputStream(Mcer), psaaword);
mOkHttpClient.newBuilder().sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
//其他配置
.build();
okHttpUtils.initClient(mOkHttpClient);
}
原生HttpClient 使用Https的代码 (以下代码只做参考)
代码语言:javascript复制 /**
*
* @param mUrl 访问的地址
* @return info
*/
public String requestHTTPSPage(String mUrl) {
InputStream ins = null;
String result = "";
try {
ins = context.getAssets().open("所在的.cer"); //下载的证书放到项目中的assets目录中
CertificateFactory cerFactory = CertificateFactory
.getInstance("X.509");
Certificate cer = cerFactory.generateCertificate(ins);
KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
keyStore.load(null, null);
keyStore.setCertificateEntry("trust", cer);
SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
Scheme sch = new Scheme("https", socketFactory, 443);
HttpClient mHttpClient = new DefaultHttpClient();
mHttpClient.getConnectionManager().getSchemeRegistry()
.register(sch);
BufferedReader reader = null;
try {
Log.d(TAG, "executeGet is in,murl:" mUrl);
HttpGet request = new HttpGet();
request.setURI(new URI(mUrl));
HttpResponse response = mHttpClient.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
request.abort();
return result;
}
reader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
result = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
if (ins != null)
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}