调用外部接口支持https请求
1,创建RestTemplateConfig.java文件,内容如下:
package com.htsec.monitor.internet.config;
import com.htsec.monitor.internet.util.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate httpsRestTemplate(HttpComponentsClientHttpRequestFactory httpsFactory) {
RestTemplate restTemplate = new RestTemplate(httpsFactory);
restTemplate.setErrorHandler(
new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) {
return false;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) {
// 默认处理非200的返回,会抛异常
}
});
return restTemplate;
}
@Bean(name = "httpsFactory")
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory()
throws Exception {
CloseableHttpClient httpClient = HttpClientUtils.acceptsUntrustedCertsHttpClient();
HttpComponentsClientHttpRequestFactory httpsFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
httpsFactory.setReadTimeout(40000);
httpsFactory.setConnectTimeout(40000);
return httpsFactory;
}
}
2,创建HttpClientUtils.java文件,内容如下:
package com.htsec.monitor.internet.util;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
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.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class HttpClientUtils {
public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
connMgr.setMaxTotal(200);
connMgr.setDefaultMaxPerRoute(100);
b.setConnectionManager( connMgr);
// finally, build the HttpClient;
// -- done!
CloseableHttpClient client = b.build();
return client;
}
}
3,创建调用类接方法,EccHttpUtils.java
package com.htsec.monitor.internet.util;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* Created by LQZ on 2019/11/13.
*/
@Slf4j
@Component
public class EccHttpUtils {
//支持https,这个地方注入的就是第1步中创建的@Bean,restTemplate这个单词无所谓,随意起名字
@Autowired
RestTemplate restTemplate;
public JSONObject doPost(String url,JSONObject requestBody) { //外部调用方法
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json");
/*requestBody.put("page", 1);
requestBody.put("size", 1);*/
HttpEntity<String> httpEntity = null;
if(requestBody!=null) {
httpEntity = new HttpEntity<String>(requestBody.toJSONString(), httpHeaders);
}else{
httpEntity = new HttpEntity<>(httpHeaders);
}
log.info("请求外部接口url="+url+",请求参数(允许null)="+requestBody);
JSONObject result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class).getBody();
log.info("请求外部接口返回result="+result);
return result;
}
}
调用外部接口支持https请求的更多相关文章
- Volley框架支持HTTPS请求。
第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...
- 支持https请求以及https请求的抓包
iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...
- Springboot 配置 ssl 实现HTTPS 请求 & Tomcat配置SSL支持https请求
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...
- iOS开发 支持https请求以及ssl证书配置(转)
原文地址:http://blog.5ibc.net/p/100221.html 众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https 楼主正好近日将http转为https,给还没 ...
- postman-SSL证书问题-支持HTTPS请求
使用Google接口调试插件postman请求https协议的接口,postman提示: 为此,需要解决这个问题,提示信息已经给出了解决方案!Using self-signed SSL certifi ...
- 【Nginx】使用certbot安装免费https证书使Nginx支持Https请求
certbot官网:https://certbot.eff.org/lets-encrypt/centosrhel7-nginx 一.安装步骤 1)安装certbot,执行 sudo yum ins ...
- HttpClient 之 发送Https请求
HttpClient包是一个优秀的Http请求的开源jar. 本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求. 但是由于项目的需要快速的实现,以下代码还可能会有点过 ...
- 让你的网站免费支持 HTTPS 及 Nginx 平滑升级
为什么要使用 HTTPS ? 首先来说一下 HTTP 与 HTTPS 协议的区别吧,他们的根本区别就是 HTTPS 在 HTTP 协议的基础上加入了 SSL 层,在传输层对网络连接进行加密.简单点说在 ...
- Qt 发送 https 请求
1.环境 ubuntu 12.04 Qt库版本 4.8.1(安装包是Nokia时期的sdk,现在已经不好找了) 2.网上一查都说 Qt 默认不支持Openssl,心想那https也肯定用不了啊,然后屁 ...
随机推荐
- 【C语言C++编程入门】——程序结构:构思!
学习编程语言的最好方法是编写程序.一般来说,初学者编写的第一个程序是一个名为"Hello World"的程序,它简单地将"Hello World"打印到你的电脑 ...
- Socket编程,C语言版!
socket编程--send函数&recv函数详解 一.send函数 ✍ 函数原型: int send( SOCKET s,char *buf,int len,int flags ); ✍ 功 ...
- zabbix安装中文语言包及中文乱码的解决(zabbix5.0)
一,zabbix不能配置中文界面的问题: 1, zabbix5.0 系统安装后,web界面不能选择使用中文 系统提示: You are not able to choose some of the l ...
- centos8使用systemctl管理运行级别
一,什么是systemd的target? 1,关于systemd/systemctl的相关知识,请移步到这一篇 https://www.cnblogs.com/architectforest/p/12 ...
- Helium文档11-WebUI自动化-attach_file上传文件或图片
前言 attach_file关键字根据官方介绍的作用是上传文件 入参介绍 def attach_file(file_path, to=None): """ :param ...
- 如何将Nginx注册为系统服务,开机自启动。
亲测有效! 一般程序员在实际工作中,除了敲代码,很少有机会实际接触操作其它东西,例如服务器环境搭建,项目部署等等,不是领导信任或项目组核心成员,应该是没有机会实际接触的,只能通过网上资料稍微了解一下. ...
- 习题解答chapter04
题目: 实验:利用IDE的debug功能给例6.4和例6.6的new语句设置断点,使用单步调试(step into/step over)跟踪子类对象实例化(初始化)的执行顺序,并总结该过程.(教材:J ...
- 深度学习四从循环神经网络入手学习LSTM及GRU
循环神经网络 简介 循环神经网络(Recurrent Neural Networks, RNN) 是一类用于处理序列数据的神经网络.之前的说的卷积神经网络是专门用于处理网格化数据(例如一个图像)的神经 ...
- JMeter实战(二) 运行原理
运行原理 如果不用工具,要做 100 并发的压力测试,得想办法组织 100 个人,每个人操作1台电脑,一声令下,100 个人同时点击,对系统造成 100 并发.现实中,很难找 100 个人和 100 ...
- Phoenix的一些问题
date: 2020-09-10 13:50:00 updated: 2020-09-14 16:30:00 1. Phoenix索引 全局索引:适合读多写少的场景.写数据时因为索引表分布在不同数据节 ...