Https绕过证书验证1
HttpPost postRequest = null; StringEntity input = null; HttpResponse response = null; BufferedReader br = null; String output = null; int timeout=600000; HttpURLConnection httpURLConnection = null; OutputStream outputStream = null; String body = ""; // 采用绕过验证的方式处理https请求 SSLContext sslcontext = createIgnoreVerifySSL(); // 设置协议http和https对应的处理socket链接工厂的对象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)).build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); // 创建自定义的httpclient对象 CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build(); // CloseableHttpClient client = HttpClients.createDefault(); try { // 创建post方式请求对象 HttpPost httpPost = new HttpPost(strRestfulURL); // 指定报文头Content-type、User-Agent // httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); // httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); input = new UrlEncodedFormEntity("参数", "utf-8"); //input = new StringEntity(new String(IOUtils.toCharArray(objRequest.getInputStream())), "utf-8"); // 设置请求的内容 httpPost.setEntity(input); httpPost.setHeader(new BasicHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8")); // 设置期望服务端返回的编码 httpPost.setHeader(new BasicHeader("Accept","text/plain;charset=utf-8")); // 执行请求操作,并拿到结果(同步阻塞) CloseableHttpResponse response1 = client.execute(httpPost); br = new BufferedReader(new InputStreamReader( (response1.getEntity().getContent()))); while ((output = br.readLine()) != null) { objResponse.getWriter().write(output); } objResponse.flushBuffer(); response1.close(); System.out.println("body:" + body); }catch( IOException e){ e.printStackTrace(); } finally { client.close(); } public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sc = SSLContext.getInstance("SSLv3"); // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法 X509TrustManager trustManager = new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; sc.init(null, new TrustManager[] { trustManager }, null); return sc; }