android http 和https请求

转载自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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值