https忽略证书错误

<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.6</version>
		</dependency>
package http;

import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

	private static CloseableHttpClient httpclient = null;
	private static BasicCookieStore cookieStore = new BasicCookieStore();

	public static final String CHARSET_UTF8 = "utf8";

	/**
	 * 获取 CloseableHttpClient
	 * 
	 * @return
	 */
	private static CloseableHttpClient getHttpClient() {
		CloseableHttpClient httpClient = null;
		httpClient = HttpClients.custom().setSslcontext(getSslcontext())
				.setDefaultRequestConfig(getRequestConfig())
				.setDefaultCookieStore(cookieStore).build();
		return httpClient;
	}

	/**
	 * 
	 * @return
	 */
	private static RequestConfig getRequestConfig() {
		RequestConfig defaultRequestConfig = RequestConfig.custom()
				.setConnectionRequestTimeout(1000).build();
		return defaultRequestConfig;

	}

	private static SSLContext getSslcontext() {
		SSLContext sslContext = null;
		try {
			sslContext = SSLContext.getInstance("TLS");
			TrustManager tm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
				}

				public void checkServerTrusted(X509Certificate[] chain,
						String authType) throws CertificateException {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			sslContext.init(null, new TrustManager[] { tm }, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sslContext;
	}

	public static String getWithCookie(String url, String charset) {
		String page = null;
		CloseableHttpResponse response = null;
		if (httpclient == null) {
			httpclient = getHttpClient();
		}
		HttpGet httpGet = new HttpGet(url);
		try {
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			page = EntityUtils.toString(entity, charset);

			EntityUtils.consume(entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return page;
	}

	public static String postWithCookie(String uri, String charset,
			List<NameValuePair> params) {
		String resStr = null;
		HttpPost httpPost = new HttpPost(uri);
		CloseableHttpResponse response = null;
		if (httpclient == null) {
			httpclient = new IgnoreCAHttpClient();
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(params));
			response = httpclient.execute(httpPost);
			System.out.println(response.getStatusLine());
			HttpEntity entity2 = response.getEntity();
			EntityUtils.toString(entity2, charset);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resStr;

	}

}


你可能感兴趣的:(https忽略证书错误)