前言:
此时我们对“java发送接收https请求”大体比较关怀,咱们都想要学习一些“java发送接收https请求”的相关资讯。那么小编也在网络上收集了一些对于“java发送接收https请求””的相关文章,希望姐妹们能喜欢,姐妹们快快来了解一下吧!一、基于自定义配置的https
import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.client.HttpClients;import org.apache.http.ssl.SSLContexts;import org.apache.http.ssl.TrustStrategy;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.ResponseEntity;import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.stereotype.Component;import org.springframework.util.LinkedMultiValueMap;import org.springframework.web.client.RestTemplate;import javax.net.ssl.SSLContext;import java.security.KeyManagementException;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.util.List;/** * 远程请求的工具类 */@Componentpublic class RestTemplateUtil { private String remoteIp; @Value("${remote.ip}") public void setRemoteIp(String remoteIp) { this.remoteIp = remoteIp; } /*** * 配置https的请求 * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws KeyStoreException */ public HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException { TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true; SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setSSLSocketFactory(connectionSocketFactory); CloseableHttpClient httpClient = httpClientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(httpClient); return factory; } public String executePost(String url,LinkedMultiValueMap paramsMap) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { String URL=remoteIp+url; RestTemplate restTemplate = new RestTemplate(generateHttpRequestFactory()); HttpHeaders headers = new HttpHeaders(); HttpEntity<LinkedMultiValueMap<String, Object>> request = new HttpEntity<>(paramsMap, headers); ResponseEntity<String> response = restTemplate.postForEntity(URL, request , String.class ); String data=response.getBody(); return data; }}
二、使用HttpUtil实现远程请求,使用的hutool依赖下的工具类
导入依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.18</version></dependency>
import cn.hutool.http.HttpUtil;Map<String,Object> map=new HashMap<>();map.put("xxx","xx");map.put("xxx","xx");map.put("xxx","xx");map.put("xxx","xx");//远程请求的接口String url=";;HttpUtil.createGet(url).form(map).timeout(1*60*1000).execute().body();
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java发送接收https请求