android http 和https请求

  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请求

原文地址:https://www.cnblogs.com/android-blogs/p/5980899.html