httpClient4.3发送https请求
2014-06-15 16:46
393 查看
首先创建一个工具类:
public class HttpClientUtil { public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial( null, new TrustStrategy() { // 信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return HttpClients.createDefault(); } }
在自己的程序中调用:
public static void main(String[] args) throws Exception { CloseableHttpClient client = HttpClientUtil.createSSLClientDefault(); HttpGet get = new HttpGet("https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2014-06-18&leftTicketDTO.from_station=WHN&leftTicketDTO.to_station=IZQ&purpose_codes=ADULT"); CloseableHttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); InputStream input = entity.getContent(); OutputStream output = new FileOutputStream("D:/dat.txt"); int len = 0; byte[] buff = new byte[1024]; while(-1!=(len = input.read(buff))){ output.write(buff, 0,len); } }
相关文章推荐
- httpClient4.1发送https报文请求,带证书,签名
- HttpClient 之 发送Https请求
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求
- httpClient发送https 请求
- HttpClient4.3 关于https 中SSL证书请求问题
- HttpClient 之 4.x.x版本以上的发送Https请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求
- Jsoup 发送post请求 org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https:
- 04_HttpClient发送Https请求
- httpClient4.1发送https报文请求,带证书,签名
- 使用spring-web中的api发送http或https请求的代码
- HttpClient学习笔记 --发送Http和Https请求
- 用httpclient发送https协议请求以及javax.net.ssl.SSLHandshakeException解决办法
- 【网络】JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
- ruby发送http,https请求
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- 使用Apache提供的HttpClient发送https请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求