android https HttpsURLConnection 忽略证书
2015-03-16 20:13
861 查看
在请求https的时候,有时候会出现各种报错,以下代码可忽略证书。
之前网上找过一些,都会有问题,这个版本是亲自验证可用的。
之前网上找过一些,都会有问题,这个版本是亲自验证可用的。
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; import android.util.Log; public class NullHostNameVerifier implements HostnameVerifier { @Override public boolean verify(String hostname, SSLSession session) { Log.i("RestUtilImpl", "Approving certificate for " + hostname); return true; } }
HttpURLConnection conn = null; // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){ public X509Certificate[] getAcceptedIssuers(){return null;} public void checkClientTrusted(X509Certificate[] certs, String authType){} public void checkServerTrusted(X509Certificate[] certs, String authType){} }}; // Install the all-trusting trust manager try {// 注意这部分一定要 HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier()); SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); URL url = new URL(notifyUrl); conn = (HttpURLConnection) url.openConnection(); conn.connect(); System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage()); } catch (Exception e) { e.printStackTrace(); }
相关文章推荐
- "Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Connection
- Android HttpURLConnection Post 参数 (https)
- Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Connection to https://dl-ssl.google.com refused
- Android HttpsUrlConnection HTTPS
- android webview 加载https --忽略证书
- Android HttpsUrlConnection eofexception --> java.io.EOFException
- 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类
- Android支持https的处理方式2----HttpURLConnection请求https处理方式
- 【Android Trick 2】HTTPS请求忽略证书
- HttpsURLConnection访问HTTPS链接时信任所有证书
- Android HttpsURLConnection get、post 访问网络
- java给HttpsURLConnection添加信任证书
- Android安全开发之正确使用Https(内含OkHttp、HttpUrlConnection配置https方式)
- 为HttpsURLConnection添加信任证书
- Android HttpUrlConnection如何使用Https连接
- Android HttpsUrlConnection eofexception 错误
- "Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Connection
- Android okhttp3.0忽略https证书的方法
- HttpsURLConnection和DefaultHttpClient连接HTTPS不用证书
- Android SSL双向认证HttpsURLConnection