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请求的更多相关文章

  1. Android进阶(二)https请求No peer certificate的解决方法.

    在做Android客户端通过https协议访问12306,并爬取数据时,出现了如下错误: 其中有一条错误提示是 javax.net.ssl.SSLPeerUnverifiedException: No ...

  2. 使用Charles对Android App的https请求进行抓包

    本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...

  3. Android 7.0解决抓取不到https请求的问题

    问题:Android7.0系统,使用fiddler不能抓取https请求 解决方法:  1.在源码res目录下新建xml目录,增加network_security_config.xml文件 (工程名/ ...

  4. 我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

    下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确,也不需要验证服务器证书中的域名是否有效. (PS:建议下 ...

  5. Charles 查看https请求数据 Mac/ android

    Charles_v4.0.1_Mac_破解版下载地址:https://pan.baidu.com/s/1c23VPuS 1.在Mac电脑上安装Charles的根证书 打开Charles->菜单H ...

  6. fiddler抓取https请求(android/ios)

    本文转载自:http://blog.csdn.net/songer_xing/article/details/53841401 备注:本人有这样的一个需求,先记录下,以后再进行整理. 在抓包过程中发现 ...

  7. Window下通过charles代理抓取iphone/android手机Https请求乱码问题处理

    Window下通过charles代理抓取iphone手机Https请求乱码问题 如果保持默认设置,https的reqeust和response都是乱码,设置完之后https就可以抓包了 手机端操作: ...

  8. Volley框架支持HTTPS请求。

    第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...

  9. android httpClient 支持HTTPS的访问方式

    项目中Android https请求地址遇到了这个异常,javax.net.ssl.SSLPeerUnverifiedException: No peer certificate,是SSL协议中没有终 ...

随机推荐

  1. IT人才最容易犯的17个错误--人生警言

    转载 记得刚参加工作时(那是97年),中国的IT刚刚兴起,那时,作为一个IT人士是一件很光荣的事,而那时的我正在做电气和电子相关的工作.99年第一次跳槽,进入了IT行业做软件开发.至今,中国的IT已经 ...

  2. UIkit框架之UItableview

    1.继承链:UIScrrollView:UIview:UIresponder:NSObject 2.创建实例的时候首先需要确定table的类型 3.一个tableview对象必须要有一个数据源和一个委 ...

  3. 深入学习:Windows下Git入门教程(上)

    一,安装Git: 1.1Linux上安装命令: sudo apt-get install git 1.2在Windows上安装Git: 使用Windows版的msysgit,官方下载地址:http:/ ...

  4. Linux----快速注释包含特定字符串的行

    常常会需要将程序中的输出语句注释,往往手工非常慢,而且容易漏. 今天研究了 linux  的 sed, 真心好用.. 例子: 将 包含 cout 的语句注释,也就是说包含cout 语句前加入字符串 / ...

  5. 数据库类II

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  6. C++面向对象编程解决三阶矩阵相加减

    /*此处用面向对象编程*/ #include<iostream> #include<string.h> using namespace std; class Matrices ...

  7. # 20145210 《Java程序设计》第06周学习总结

    教材学习内容总结 第十章 输入\输出 10.1 InputStream与OutputStream •串流设计的概念 •java将输入\输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象 •从应 ...

  8. iBatis框架简介

    一.为啥使用iBatis? 在 Hibernate.JPA 这样的一站式对象 / 关系映射(O/R Mapping)解决方案盛行之前,iBaits 基本是持久层框架的不二选择.即使在持久层框架层出不穷 ...

  9. HDU 4386

    http://acm.hdu.edu.cn/showproblem.php?pid=4386 题意:给四条边长,问能否组成四边形,如果能,求最大面积 求最大面积用海伦公式的四边形推广,p=(a+b+c ...

  10. 服务器的Arch Linux,CentOS的,Debian的,Fedora的,Gentoo的,openSUSE的,Slackware的,和Ubuntu哪个好

    我能够建议的就是:如果你自己是开发者,如果你自己买了一台 VPS 自己搭服务器用.选 Ubuntu/Debian 挺好.当然如果你觉得自己闲工夫实在多得没处花,可以隔三差五的就到服务器上做升级更新,用 ...