技术文档

JAVA连接HTTPS – HTTPS SSL 教程

使用Apache的HttpClient 和 HttpCore

JAVA连接HTTPS – HTTPS SSL 教程

https://hc.apache.org

packagecn.getssl.https; importjava.io.BufferedReader; importjava.io.InputStreamReader; import.ssl.SSLContext; importorg.apache.http.HttpEntity; importorg.apache.http.client.methods.CloseableHttpResponse; importorg.apache.http.client.methods.HttpGet; importorg.apache.http.conn.ssl.SSLConnectionSocketFactory; importorg.apache.http.impl.client.CloseableHttpClient; importorg.apache.http.impl.client.HttpClients; importorg.apache.http.ssl.SSLContexts; importorg.apache.http.util.EntityUtils; publicclassConnectionHttps{ publicstaticvoidmain(String[]args)throwsException{ //TODOAuto-generatedmethodstub SSLContextsslcontext=SSLContexts.createSystemDefault(); //AllowTLSv1protocolonly SSLConnectionSocketFactorysslsf=newSSLConnectionSocketFactory( sslcontext, newString[]{"TLSv1"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); CloseableHttpClienthttpclient=HttpClients.custom() .setSSLSocketFactory(sslsf) .build(); try{ HttpGethttpget=newHttpGet("https://www.baidu.com/"); System.out.println("Executingrequest"+httpget.getRequestLine()); CloseableHttpResponseresponse=httpclient.execute(httpget); try{ HttpEntityentity=response.getEntity(); BufferedReaderin=newBufferedReader(newInputStreamReader(entity.getContent())); while(in.ready()){ System.out.println(in.readLine()); } System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(entity); }finally{ response.close(); } }finally{ httpclient.close(); } } }

 

©2020-2024   万云SSL证书  (www.sslssl.com.cn)  万云科技   豫ICP备2020034358号-10