android http 和https请求
private static final int CONNECTION_TIMEOUT = 10000; public static String doHttpGet(String serverURL) throws Exception {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(serverURL);
get.addHeader("Content-Type", "text/xml");
get.setParams(httpParameters);
HttpResponse response = null;
try {
response = hc.execute(get);
} catch (UnknownHostException e) {
throw new Exception("Unable to access " + e.getLocalizedMessage());
} catch (SocketException e) {
throw new Exception(e.getLocalizedMessage());
}
int sCode = response.getStatusLine().getStatusCode();
if (sCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else
throw new Exception("StatusCode is " + sCode);
} public static String doHttpsGet(String serverURL) throws Exception {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpClient hc = initHttpClient(httpParameters);
HttpGet get = new HttpGet(serverURL);
get.addHeader("Content-Type", "text/xml");
get.setParams(httpParameters);
HttpResponse response = null;
try {
response = hc.execute(get);
} catch (UnknownHostException e) {
throw new Exception("Unable to access " + e.getLocalizedMessage());
} catch (SocketException e) {
throw new Exception(e.getLocalizedMessage());
}
int sCode = response.getStatusLine().getStatusCode();
if (sCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else
throw new Exception("StatusCode is " + sCode);
} public static String doHttpPost(String serverURL, String xmlString) throws Exception {
Log.d("doHttpPost", "serverURL="+serverURL);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost(serverURL);
post.addHeader("Content-Type", "text/xml");
post.setEntity(new StringEntity(xmlString, "UTF-8"));
post.setParams(httpParameters);
HttpResponse response = null;
try {
response = hc.execute(post);
} catch (UnknownHostException e) {
throw new Exception("Unable to access " + e.getLocalizedMessage());
} catch (SocketException e) {
throw new Exception(e.getLocalizedMessage());
}
int sCode = response.getStatusLine().getStatusCode();
Log.d("response code ", "sCode="+sCode);
if (sCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else
throw new Exception("StatusCode is " + sCode);
} public static String doHttpsPost(String serverURL, String xmlString) throws Exception {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpClient hc = initHttpClient(httpParameters);
HttpPost post = new HttpPost(serverURL);
post.addHeader("Content-Type", "text/xml");
post.setEntity(new StringEntity(xmlString, "UTF-8"));
post.setParams(httpParameters);
HttpResponse response = null;
try {
response = hc.execute(post);
} catch (UnknownHostException e) {
throw new Exception("Unable to access " + e.getLocalizedMessage());
} catch (SocketException e) {
throw new Exception(e.getLocalizedMessage());
}
int sCode = response.getStatusLine().getStatusCode();
if (sCode == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
} else
throw new Exception("StatusCode is " + sCode);
} public static HttpClient initHttpClient(HttpParams params) {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryImp(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient(params);
}
} public static class SSLSocketFactoryImp extends SSLSocketFactory {
final SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryImp(KeyStore truststore) throws NoSuchAlgorithmException,
KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore); TrustManager tm = new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
} @Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
} @Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
}
};
sslContext.init(null, new TrustManager[] {
tm
}, null);
} @Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
} @Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
android http 和https请求
android http 和https请求的更多相关文章
- Android进阶(二)https请求No peer certificate的解决方法.
在做Android客户端通过https协议访问12306,并爬取数据时,出现了如下错误: 其中有一条错误提示是 javax.net.ssl.SSLPeerUnverifiedException: No ...
- 使用Charles对Android App的https请求进行抓包
本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...
- Android 7.0解决抓取不到https请求的问题
问题:Android7.0系统,使用fiddler不能抓取https请求 解决方法: 1.在源码res目录下新建xml目录,增加network_security_config.xml文件 (工程名/ ...
- 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类
下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...
- Charles 查看https请求数据 Mac/ android
Charles_v4.0.1_Mac_破解版下载地址:https://pan.baidu.com/s/1c23VPuS 1.在Mac电脑上安装Charles的根证书 打开Charles->菜单H ...
- fiddler抓取https请求(android/ios)
本文转载自:http://blog.csdn.net/songer_xing/article/details/53841401 备注:本人有这样的一个需求,先记录下,以后再进行整理. 在抓包过程中发现 ...
- Window下通过charles代理抓取iphone/android手机Https请求乱码问题处理
Window下通过charles代理抓取iphone手机Https请求乱码问题 如果保持默认设置,https的reqeust和response都是乱码,设置完之后https就可以抓包了 手机端操作: ...
- Volley框架支持HTTPS请求。
第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...
- android httpClient 支持HTTPS的访问方式
项目中Android https请求地址遇到了这个异常,javax.net.ssl.SSLPeerUnverifiedException: No peer certificate,是SSL协议中没有终 ...
随机推荐
- Chapter 4: Tomcat Default Connector
一.概述 第三章介绍的connector是一个很好的学习工具,但是我们还可以做的更多.这一章介绍的是Tomcat4默认的connector. 一个Tomcat的connector是一个独立的模块,能够 ...
- Unix command 积累
UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platfo ...
- 【LeetCode OJ】Pascal's Triangle II
Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...
- Inno Setup使用上的几个问题
Inno Setup使用上的几个问题 分类: Install Setup 2013-02-02 15:48 1781人阅读 评论(0) 收藏 举报 Inno Setup使用上的几个问题: [问题一:I ...
- JQuery 实现倒计时
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第二个Sprint冲刺第二天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 任务:解决了第二个Sprint冲刺第一天遇到的错误. 燃尽图: 遇到的问题: 解决之后: 开发感悟:最近一直在写代码,都很少外出活动了,不知不觉 ...
- i++为什么没有自增探析——JVM中i++的实现(转)
很多朋友在使用Java时候会发现一个很奇怪的现象. 那就是使用下列的短句时会发现i没有自增,这是很让人迷惑的,因为大家印象中,虽然i++优先级较低,但是总是会自增的,这里为什么i++没有自增? i=i ...
- Think Python - Chapter 03 - Functions
3.1 Function callsIn the context of programming, a function is a named sequence of statements that p ...
- VIM键盘快捷键映射
http://www.jianshu.com/p/216811be226b
- jquery获取所有选中的checkbox的ID
//获取所有选中的CheckBox的id function getCheckBox() { var spCodesTemp = ""; $("input:checkbox ...