android http 和https请求

摘要:
1私有静态终端连接_超时=10000;23publicstaticStringdoHttpGet(StringserverURL)throwsException{4HttpParamshttpParameters=newBasicHttpParams();5HttpConnectionParams.setConnectionTimeout(h
  1 private static final int CONNECTION_TIMEOUT = 10000;
  2 
  3     public static String doHttpGet(String serverURL) throws Exception {
  4         HttpParams httpParameters = new BasicHttpParams();
  5         HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
  6         HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
  7         HttpClient hc = new DefaultHttpClient();
  8         HttpGet get = new HttpGet(serverURL);
  9         get.addHeader("Content-Type", "text/xml");
 10         get.setParams(httpParameters);
 11         HttpResponse response = null;
 12         try {
 13             response = hc.execute(get);
 14         } catch (UnknownHostException e) {
 15             throw new Exception("Unable to access " + e.getLocalizedMessage());
 16         } catch (SocketException e) {
 17             throw new Exception(e.getLocalizedMessage());
 18         }
 19         int sCode = response.getStatusLine().getStatusCode();
 20         if (sCode == HttpStatus.SC_OK) {
 21             return EntityUtils.toString(response.getEntity());
 22         } else
 23             throw new Exception("StatusCode is " + sCode);
 24     }
 25 
 26     public static String doHttpsGet(String serverURL) throws Exception {
 27         HttpParams httpParameters = new BasicHttpParams();
 28         HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
 29         HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
 30         HttpClient hc = initHttpClient(httpParameters);
 31         HttpGet get = new HttpGet(serverURL);
 32         get.addHeader("Content-Type", "text/xml");
 33         get.setParams(httpParameters);
 34         HttpResponse response = null;
 35         try {
 36             response = hc.execute(get);
 37         } catch (UnknownHostException e) {
 38             throw new Exception("Unable to access " + e.getLocalizedMessage());
 39         } catch (SocketException e) {
 40             throw new Exception(e.getLocalizedMessage());
 41         }
 42         int sCode = response.getStatusLine().getStatusCode();
 43         if (sCode == HttpStatus.SC_OK) {
 44             return EntityUtils.toString(response.getEntity());
 45         } else
 46             throw new Exception("StatusCode is " + sCode);
 47     }
 48 
 49     public static String doHttpPost(String serverURL, String xmlString) throws Exception {
 50         Log.d("doHttpPost", "serverURL="+serverURL);
 51         HttpParams httpParameters = new BasicHttpParams();
 52         HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
 53         HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
 54         HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
 55         HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
 56         HttpClient hc = new DefaultHttpClient();
 57         HttpPost post = new HttpPost(serverURL);
 58         post.addHeader("Content-Type", "text/xml");
 59         post.setEntity(new StringEntity(xmlString, "UTF-8"));
 60         post.setParams(httpParameters);
 61         HttpResponse response = null;
 62         try {
 63             response = hc.execute(post);
 64         } catch (UnknownHostException e) {
 65             throw new Exception("Unable to access " + e.getLocalizedMessage());
 66         } catch (SocketException e) {
 67             throw new Exception(e.getLocalizedMessage());
 68         }
 69         int sCode = response.getStatusLine().getStatusCode();
 70         Log.d("response code ", "sCode="+sCode);
 71         if (sCode == HttpStatus.SC_OK) {
 72             return EntityUtils.toString(response.getEntity());
 73         } else
 74             throw new Exception("StatusCode is " + sCode);
 75     }
 76 
 77     public static String doHttpsPost(String serverURL, String xmlString) throws Exception {
 78         HttpParams httpParameters = new BasicHttpParams();
 79         HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
 80         HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT);
 81         HttpClient hc = initHttpClient(httpParameters);
 82         HttpPost post = new HttpPost(serverURL);
 83         post.addHeader("Content-Type", "text/xml");
 84         post.setEntity(new StringEntity(xmlString, "UTF-8"));
 85         post.setParams(httpParameters);
 86         HttpResponse response = null;
 87         try {
 88             response = hc.execute(post);
 89         } catch (UnknownHostException e) {
 90             throw new Exception("Unable to access " + e.getLocalizedMessage());
 91         } catch (SocketException e) {
 92             throw new Exception(e.getLocalizedMessage());
 93         }
 94         int sCode = response.getStatusLine().getStatusCode();
 95         if (sCode == HttpStatus.SC_OK) {
 96             return EntityUtils.toString(response.getEntity());
 97         } else
 98             throw new Exception("StatusCode is " + sCode);
 99     }
100 
101     public static HttpClient initHttpClient(HttpParams params) {
102         try {
103             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
104             trustStore.load(null, null);
105 
106             SSLSocketFactory sf = new SSLSocketFactoryImp(trustStore);
107             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
108 
109             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
110             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
111 
112             SchemeRegistry registry = new SchemeRegistry();
113             registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
114             registry.register(new Scheme("https", sf, 443));
115 
116             ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
117 
118             return new DefaultHttpClient(ccm, params);
119         } catch (Exception e) {
120             return new DefaultHttpClient(params);
121         }
122     }
123 
124     public static class SSLSocketFactoryImp extends SSLSocketFactory {
125         final SSLContext sslContext = SSLContext.getInstance("TLS");
126 
127         public SSLSocketFactoryImp(KeyStore truststore) throws NoSuchAlgorithmException,
128                 KeyManagementException, KeyStoreException, UnrecoverableKeyException {
129             super(truststore);
130 
131             TrustManager tm = new X509TrustManager() {
132                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
133                     return null;
134                 }
135 
136                 @Override
137                 public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
138                         String authType) throws java.security.cert.CertificateException {
139                 }
140 
141                 @Override
142                 public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
143                         String authType) throws java.security.cert.CertificateException {
144                 }
145             };
146             sslContext.init(null, new TrustManager[] {
147                 tm
148             }, null);
149         }
150 
151         @Override
152         public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
153                 throws IOException, UnknownHostException {
154             return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
155         }
156 
157         @Override
158         public Socket createSocket() throws IOException {
159             return sslContext.getSocketFactory().createSocket();
160         }
161     }
android http 和https请求

免责声明:文章转载自《android http 和https请求》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇cuda8和cuda10切换 以及正确按照配置cuDNN不定字段数目的数据库表设计和数据结构下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信支付方式区分

第一: JSAPI支付     https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1 使用场景:JSAPI支付是指商户通过调用微信支付提供的JSAPI接口,在支付场景中调起微信支付模块完成收款 使用实例 A:线下实例   B:公众号场景   C:PC网站场景            A: ...

Springcloud之gateway配置及swagger集成

前言 关于引入gateway的好处我网上找了下: 性能:API高可用,负载均衡,容错机制。 安全:权限身份认证、脱敏,流量清洗,后端签名(保证全链路可信调用),黑名单(非法调用的限制)。 日志:日志记录(spainid,traceid)一旦涉及分布式,全链路跟踪必不可少。 缓存:数据缓存。监控:记录请求响应数据,api耗时分析,性能监控。 限流:流量控制...

[转]Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误-高级

from : https://levy.at/blog/11 进阶篇 关闭HTTP的响应 level: intermediate 当你使用标准http库发起请求时,你得到一个http的响应变量。如果你不读取响应主体,你依旧需要关闭它。注意对于空的响应你也一定要这么做。对于新的Go开发者而言,这个很容易就会忘掉。 一些新的Go开发者确实尝试关闭响应主体,但...

statement 、prepareStatement的用法和解释

转自:http://blog.csdn.net/QH_JAVA/article/details/48245945 一、prepareStatement 的用法和解释 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Stateme...

Springcloud 学习笔记04-Springboot连接数据库

一、打开user-service微服务 启动类右键Run’UserServiceApplication’或者点击右上方启动 2 Springboot连接Mysql数据库 (1)首先我们将需要的包导入,这几个包都是我们稍后要用到的: <dependency> <groupId>mysql<...

SpringBoot+Mybatis-Plus两种分页方法

用到的依赖: <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}&l...