android http 和https请求
2016-12-21 16:33
357 查看
转载自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();
} }
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 HTTPURLConnection解决不能访问HTTPs请求
- android HTTPURLConnection解决不能访问Https请求
- Android如何构建一个https请求的httpClient
- android 网络协议等socket,http,HTTPS,get。post请求 等详解
- 开源库android-async-http添加https请求方式
- Android支持https的处理方式1----httpclient请求https的处理方式
- 抓取手机(ios/android)https&http请求报文
- android HTTPURLConnection解决不能访问HTTPs请求
- 使用burpsuite监控设备(如ios,android)网络http,https请求
- Android支持https的处理方式2----HttpURLConnection请求https处理方式
- Android OkHttp及http、https网络请求方法
- Android中原http请求的https实现(URLConnection 、volley)(volley不修改源码)
- Android-OKhttp解决https安全链接请求问题
- android 5.0之后原生webview同时用https请求不了http的资源图片解决方案
- Android中HttpClient请求https的验证过程
- android http 和https请求
- android http 和https请求
- android https——okhttp实现https请求
- 开源库android-async-http添加https请求方式
- Android HTTP请求访问的方法