httpclient 实现https请求
httpclient 实现https请求,4.4版本之后,工具类如下:
package com.auth.verification;import java.io.IOException;import java.security.KeyManagementException;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLContext;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.TrustStrategy;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.ssl.SSLContextBuilder;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class HttpsUtils { private static Logger logger = LoggerFactory.getLogger(HttpsUtils.class); static CloseableHttpClient httpClient; static CloseableHttpResponse httpResponse; public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // 信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { retu true; } }).build(); HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); retu HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } retu HttpClients.createDefault(); } /** * 发送https请求 * * @param jsonPara * @throws Exception */ public static String sendByHttp(Map<String, Object> params, String url) { try { HttpPost httpPost = new HttpPost(url); List<NameValuePair> listNVP = new ArrayList<NameValuePair>(); if (params != null) { for (String key : params.keySet()) { listNVP.add(new BasicNameValuePair(key, params.get(key).toString())); } } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(listNVP, "UTF-8"); logger.info("创建请求httpPost-URL={},params={}", url, listNVP); httpPost.setEntity(entity); httpClient = HttpsUtils.createSSLClientDefault(); httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { String jsObject = EntityUtils.toString(httpEntity, "UTF-8"); retu jsObject; } else { retu null; } } catch (Exception e) { e.printStackTrace(); retu null; } finally { try { httpResponse.close(); httpClient.close(); logger.info("请求流关闭完成"); } catch (IOException e) { logger.info("请求流关闭出错"); e.printStackTrace(); } } } public static void main(String[] args) throws Exception { Map<String, Object> map = new HashMap<>(); map.put("authCode", "FX:123"); map.put("userName", "jianghaida"); map.put("pwd", "jianghaida"); System.out.println(HttpsUtils.sendByHttp(map, "https://localhost:8010/postDoc"));; }}
4.4之前的版本 可参考 http://blog.csdn.net/rongyongfeikai2/article/details/41659353/
代码都以测试过,可用
作者:流云风
来源链接:https://blog.csdn.net/Cloud_July/article/details/73301805
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。