Android访问https服务

实现代码:


package com.fyfeng.httpstest;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URL;
import java.security.Principal;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;


public class MainActivity extends Activity {

private static final String tag = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


View button = this.findViewById(R.id.button);


button.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View v) {
onclick();
}});
}


private void onclick(){

new Thread(new Runnable(){


@Override
public void run() {
try{
String str = "https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2F";
URL url = new URL(str);

SSLContext sslctxt = SSLContext.getInstance("TLS");

sslctxt.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());

HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();

conn.setSSLSocketFactory(sslctxt.getSocketFactory());
conn.setHostnameVerifier(new MyHostnameVerifier());

conn.connect();

int respCode = conn.getResponseCode();
Log.d(tag, "ResponseCode="+respCode);
InputStream input = conn.getInputStream();


String result = toString(input);

Log.d(tag, "result:"+result);

input.close();

conn.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
private String toString(InputStream input){

String content = null;
try{
InputStreamReader ir = new InputStreamReader(input);
BufferedReader br = new BufferedReader(ir);

StringBuilder sbuff = new StringBuilder();
while(null != br){
String temp = br.readLine();
if(null == temp)break;
sbuff.append(temp).append(System.getProperty("line.separator"));
}

content = sbuff.toString();
}catch(Exception e){
e.printStackTrace();
}

return content;
}
}).start();;
}





static class MyX509TrustManager implements X509TrustManager{


@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
if(null != chain){
for(int k=0; k < chain.length; k++){
X509Certificate cer = chain[k];
print(cer);
}
}
Log.d(tag, "check client trusted. authType="+authType);

}


@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
if(null != chain){
for(int k=0; k < chain.length; k++){
X509Certificate cer = chain[k];
print(cer);
}
}
Log.d(tag, "check servlet trusted. authType="+authType);
}


@Override
public X509Certificate[] getAcceptedIssuers() {

Log.d(tag, "get acceptedissuer");

return null;
}


private void print(X509Certificate cer){

int version = cer.getVersion();
String sinname = cer.getSigAlgName();
String type = cer.getType();
String algorname = cer.getPublicKey().getAlgorithm();
BigInteger serialnum = cer.getSerialNumber();
Principal principal = cer.getIssuerDN();
String principalname = principal.getName();

Log.d(tag, "version="+version+", sinname="+sinname+", type="+type+", algorname="+algorname+", serialnum="+serialnum+", principalname="+principalname);
}

}

static class MyHostnameVerifier implements HostnameVerifier{


@Override
public boolean verify(String hostname, SSLSession session) {
Log.d(tag, "hostname="+hostname+",PeerHost= "+session.getPeerHost());
return true;
}

}


}



示例代码地址:http://download.csdn.net/detail/yzwlord/5828209





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值