现在google play对https有比较严格的审核,所以不能用httpclient来跳过https验证
参考文章:
1.https://developer.android.com/training/articles/security-ssl.html#HttpsExample
2.http://www.codeproject.com/Articles/826045/Android-security-Implementation-of-Self-signed-SSL
public class HttpsRequestUtil {
/**
* @param url
* @return result
*/
private static final int SET_CONNECTION_TIMEOUT = 15 * 1000;
private static final int SET_READ_TIMEOUT = 15 * 1000;
private static final int SET_SOCKET_TIMEOUT = 15 * 1000;
public static String doHttpsRequest(String requestURL, Context mContext,String cerFile ){
MyLogCat.d(Constants.TAG,"doHttpsRequest requestURL = " + requestURL);
String resutl = null;
CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");
AssetManager am = mContext.getAssets();
InputStream caInput = new BufferedInputStream(am.open(cerFile));
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
// System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
try {
caInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
// Tell the URLConnection to use a SocketFactory from our SSLContext
/*URL url = new URL("https://certs.cac.washington.edu/CAtest/");*/
javax.net.ssl.SSLSocketFactory sslSocket = context.getSocketFactory();
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
URL url = new URL(requestURL);
HttpsURLConnection urlConnection =
(HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
urlConnection.setHostnameVerifier(hostnameVerifier);
urlConnection.setConnectTimeout(SET_CONNECTION_TIMEOUT);
urlConnection.setReadTimeout(SET_READ_TIMEOUT);
InputStream in = urlConnection.getInputStream();
//copyInputStreamToOutputStream(in, System.out);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sb!=null){
resutl = sb.toString();
}
} catch (CertificateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (KeyManagementException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MyLogCat.d(Constants.TAG,"doHttpsRequest reuslt = " + resutl);
return resutl;
}
}