android http 和https请求
来源:互联网 发布:怎么能做网络写手 编辑:程序博客网 时间:2024/06/02 05:25
转载自http://www.cnblogs.com/zhuqiang/p/3623786.htmlprivate 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(); } }
0 0
- android http 和https请求
- Http 和 Https 访问请求
- 发起https和http请求
- haproxy 中的http请求和https请求
- https请求和http请求的转换
- java http请求和https请求
- android json实现网络请求 和普通的http请求 还有https请求安全认证
- HTTP和HTTPS的请求和响应
- http 和 https 的请求区别
- 网络请求之HTTP和HTTPS
- java发送HTTP和HTTPS请求
- java 发送http和https请求
- php curl请求http和https
- 发送HTTP和HTTPS(单向验证)请求(包含POST请求)
- ajax笔记1--HTTP,HTTPS和HTTP请求概念介绍
- http与https请求
- https http请求 demo
- http与https请求
- 2)mysql 单表的增删改查,以及子查询,关联查询
- angularJS的$watch失效问题的解决方案
- MD5加密——3行代码
- NSCache
- (实用篇)PHP不用递归遍历目录下所有文件的代码
- android http 和https请求
- Java 安全模型介绍
- 算法篇-4-动态规划-凸多边形最优三角剖分&图像压缩最优分段&电路布线
- ajax和普通请求使用spring mvc在controller中的异常统一处理
- iOS用自签名证书实现HTTPS请求的原理实例讲解
- Java删除ArrayList中的重复元素的2种方法
- 浅析MongoDB数据库的海量数据存储应用
- 自定义下拉列表框CustomSpinner
- Linux下history命令用法