关于双向认证,使用HttpsURLConnection访问Https安全链接
2017-09-07 15:14
701 查看
一、 概述
最近在Android开发群里听小伙伴们问道了一个关于OkHttp加了证书能访问到Https的url,但是访问不到Http的url,对此,当时想到的是实例化两个OkHttp的对象,一个加证书访问,另一个不加证书去访问,自己也没有实践,也只是凭空想象,不知道你们是不是也是这样想的呢?言归正传,今天就能讲讲自己用HttpsUrlConnection来访问证书链接。
二、核心代码
精简一下代码:这里我写了一个工具类,ParseUtils。 - 解析json - 解析图片 - 解析xml
public class ParseUtils { public final static String HOST = "cil.vicenter"; /** * 解析公用方法(含有验证证书部分) * @param ctx * @param result * @param url * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static String ParseResult(Context ctx,String result, URL url) throws IOException, NoSuchAlgorithmException, KeyManagementException { HttpsURLConnection conn; conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(HttpUrlConnSSLSocketFactory .getSSLSocketFactory(ctx)); conn.setConnectTimeout(5 * 1000); conn.setReadTimeout(10 * 1000); conn.setHostnameVerifier(new AllowAllHostnameVerifier()); conn.addRequestProperty("Accept-Language", Locale.getDefault().toString()); conn.addRequestProperty("Host", HOST); conn.addRequestProperty("Connection", "Keep-Alive"); conn.addRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.addRequestProperty("loginType", "mobile"); conn.connect(); if (conn.getResponseCode() == 200) { InputStreamReader isr = new InputStreamReader( conn.getInputStream()); BufferedReader bufferReader = new BufferedReader(isr); String inputLine = ""; while ((inputLine = bufferReader.readLine()) != null) { result += inputLine + "\n"; } } Log.e(ctx.getClass().getSimpleName(), result); return result; } /** * 解析图片方法(含有验证证书部分) * @param ctx * @param result * @param url * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static Bitmap ParsePicResult(Context ctx, URL url) throws IOException, NoSuchAlgorithmException, KeyManagementException { Bitmap bitmap; HttpsURLConnection conn; conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(HttpUrlConnSSLSocketFactory .getSSLSocketFactory(ctx)); conn.setConnectTimeout(30000); conn.setHostnameVerifier(new AllowAllHostnameVerifier()); conn.addRequestProperty("Accept-Language", Locale.getDefault().toString()); conn.addRequestProperty("Host", HOST); conn.addRequestProperty("Connection", "Keep-Alive"); conn.addRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.addRequestProperty("loginType", "mobile"); conn.connect(); if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); // Get bitmap through image path BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = false; //对图像不进行真正的解码 newOpts.inPurgeable = true; newOpts.inInputShareable = true; // Do not compress newOpts.inSampleSize = 4; newOpts.inPreferredConfig = Config.RGB_565; bitmap = BitmapFactory.decodeStream(is,null, newOpts); is.close(); return bitmap; } return null; } /** * 解析公用方法(含有验证证书部分) * @param ctx * @param result * @param url * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static InputStream ParseXmlResult(Context ctx,String result, URL url,float outWidth, float outHeight) throws IOException, NoSuchAlgorithmException, KeyManagementException { HttpsURLConnection conn; conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(HttpUrlConnSSLSocketFactory .getSSLSocketFactory(ctx)); conn.setConnectTimeout(5 * 1000); conn.setReadTimeout(10 * 1000); conn.setHostnameVerifier(new AllowAllHostnameVerifier()); conn.addRequestProperty("Accept-Language", Locale.getDefault().toString()); conn.addRequestProperty("Host", HOST); conn.addRequestProperty("Connection", "Keep-Alive"); conn.addRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.addRequestProperty("loginType", "mobile"); conn.connect(); InputStream is = conn.getInputStream(); return is; }
三、实现
这里实现很简单,我用的是AsyncTask异步加载1. 加载json数据 private void runHttpsRequestWithHttpsURLConnection() { AsyncTask<String, Void, String> testTask = new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { String result = ""; HttpsURLConnection conn = null; try { URI suspectListUri = ParamUtls.getSuspectListUri( BaseActivity.BaseHost,BaseActivity.BasePort); URL url = new URL(suspectListUri.toString()); result = ParseUtils.ParseResult(mContext, result, url); return result; } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); }}; testTask.execute(); } 2. 加载图片部分 String suspetPicUri = ParamUtls.getSuspetPicUri( BaseActivity.BaseHost, port, checkInfo, ParamUtls.SMALL_ICON_TYPE_POSTFIX); ImageLoader.getInstance().displayImage(suspetPicUri, holder.ivImg, OptionsUtils.getHeadOptions()); 3. 加载xml似乎没用到但是绝对也能用的
四、源码部分
看了整个实现是不是很简单呢由于博主写博客资历太浅,上传不了源码,需要的可以留言给我,留下自己的邮箱也可以。
相关文章推荐
- Android安全开发之正确使用Https(内含OkHttp、HttpUrlConnection配置https方式)
- J2EE 关于WebLogic下应用使用URL.openConnection获取连接返回 HttpsURLConnection与SOAPHttpsURLConnection的问题
- 关于WebLogic下应用使用URL.openConnection获取连接返回 HttpsURLConnection与SOAPHttpsURLConnection的问题
- 使用HttpsURLConnection访问https资源(自签名)
- 关于webview 中使用https 链接 总是弹出认证ssl问题
- J2EE 关于WebLogic下应用使用URL.openConnection获取连接返回 HttpsURLConnection与SOAPHttpsURLConnection的问题
- Android SSL双向认证HttpsURLConnection
- J2EE 关于WebLogic下应用使用URL.openConnection获取连接返回 HttpsURLConnection与SOAPHttpsURLConnection的问题
- HttpsURLConnection访问HTTPS链接时信任所有证书
- 使用HttpURLConnection访问web页面
- Android在使用HttpClient访问https时认证策略
- AS3使用HTTPService类访问需要HTTP Basic Authentication认证的URL
- 在J2ME中使用Socket通过cmwap接入点访问安全HTTPS
- 使用HttpsURLConnection发送POST请求
- 使用HttpURLConnection访问HTTP资源
- Windows下Libvirt Java API使用教程(三)- TLS认证访问和动态链接文件依赖 推荐
- 如何使用WebView访问https的url——处理SslError
- 关于Java.net.URL对象使用Proxy访问Internet资源
- Android 使用WebView访问https的url——处理SslError