标签:
所需jar:commons-logging-1.1.3.jar、httpclient-4.3.1.jar、httpcore-4.3.jar
package com.ljq.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.CoreConnectionPNames; /** * * @author Administrator * */ public class ImageTest { public static void main(String[] args) throws IOException { FileOutputStream outStream = null; String url = "https://ipcrs.pbccrc.org.cn/imgrc.do?a=1439538233825"; String host = "ipcrs.pbccrc.org.cn"; try { InputStream inStream = doGetByHttps(url,host); System.out.println(inStream); } catch (Exception e) { e.printStackTrace(); } } private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } private static class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } } public static InputStream doGetByHttps(String url, String sHost) throws Exception { long time = System.currentTimeMillis(); HttpResponse response = null; DefaultHttpClient client = null; InputStream oInput = null; SSLContext oCtx = SSLContext.getInstance("TLS"); oCtx.init(null, new TrustManager[]{new TrustAnyTrustManager()}, null); SSLSocketFactory socketFactory = new SSLSocketFactory(oCtx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", 443, socketFactory)); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry); connManager.setDefaultMaxPerRoute(20); HttpHost host = new HttpHost(sHost); connManager.setMaxForRoute(new HttpRoute(host), 20); client = new DefaultHttpClient(connManager); HttpGet oGet = new HttpGet(url); oGet.setHeader("Accept-Charset", "utf-8"); oGet.setHeader("Content-type", "application/x-www-form-urlencoded"); setTimedOut(client); response = client.execute(oGet); oInput = response.getEntity().getContent(); return oInput; } /** * set Timedout * * @param connection * @return */ private static void setTimedOut(HttpClient httpclient) { httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); } }
标签:
原文地址:http://www.cnblogs.com/linjiqin/p/4736555.html