DEAD MAN CODING

FOOOLING.COM

Java发送不安全的HTTPS

June 23, 2016, 11:22 a.m.

如何将HTTPS当HTTP用?



            SSLConnectionSocketFactory sslSF = new SSLConnectionSocketFactory(builder.build(),
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSF).build();

            HttpGet httpGet = new HttpGet("https://" + ip + ":" + port + "/xxx/xxxx");


            try {
                HttpResponse response = httpClient.execute(httpGet);
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                ByteArrayOutputStream result = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                System.out.println(result.toString("UTF-8"));
                EntityUtils.consume(entity);
            } finally {
                httpClient.close();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }


友情链接