【Android Trick 2】HTTPS请求忽略证书
现在很多网站的请求都需要使用HTTPS验证,查看google官方的HTTPS请求中可以
发现很蛋疼的会有一个keystore对象,也就是要一个证书(CA),但是如果你没有一
个有效的SSL certificate,你可能想让你的APP去忽略certificate的验证,这里将列出
两种情况下的做法:
1、HTTPSConnection
我们都知道在发送http请求时用的是HTTPConnection,但是发送https请求时使用的是
HTTPSConnection,下面看一下具体做法
首先是创建一个方法来初始化
public static String sendData(String url, List<NameValuePair> datas) { HttpClient client = createHttpClient(); HttpPost post = new HttpPost(url); HttpResponse resp = null; String result = ""; try { post.setEntity(new UrlEncodedFormEntity(datas, HTTP.UTF_8)); resp = client.execute(post); result = EntityUtils.toString(resp.getEntity()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }