转载请注明出处,谢谢http://blog.csdn.net/devilnov/article/details/13507571
使用http1.1访问https时遇到的问题
问题1、访问不到数据,这时首先要设置HttpClient的证书
HttpClient httpClient = getHttpClient(); //看后面的附加代码
HttpResponse httpResponse = httpClient.execute(httpRequest);
问题2、请求到的数据过大,这种情况,可以使用http1.0访问https和http都没问题,http1.1访问http数据也没问题,就是在http1.1访问https时有问题,这时就要压缩数据,我们使用gzip压缩请求数据
HttpGet httpRequest = new HttpGet(url);
httpRequest.setHeader("Accept-Encoding", "gzip");
然后解压缩返回的数据,OK,问题解决
InputStream is = httpResponse.getEntity().getContent(); Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { is = new GZIPInputStream(new BufferedInputStream(is)); StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n;(n = is.read(b)) != -1;) { out.append(new String(b, 0, n)); } strResult = out.toString(); }
附上代码:
1-重写SSLSocketFactory
import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.conn.ssl.SSLSocketFactory; public class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(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(); } }
protected static HttpClient getHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams httpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, HTTP_CONNECTION_TIMEOUT); HttpConnectionParams.setSocketBufferSize(httpParams, HTTP_BUFFER_SIZE); HttpClientParams.setRedirecting(httpParams, true); String userAgent = Build.MODEL; HttpProtocolParams.setUserAgent(httpParams, userAgent); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); schemeRegistry.register(new Scheme("https", sf, HTTPS_PORT)); ClientConnectionManager connManager = new ThreadSafeClientConnManager( httpParams, schemeRegistry); return new DefaultHttpClient(connManager, httpParams); } catch (Exception e) { // TODO Auto-generated catch block return new DefaultHttpClient(); } }