使用restTemplate来访问https
1、maven: <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
2、@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
}
测试:
public String getData()
{
//接口地址
String url = "https://free-api.heweather.com/v5/forecast?city=CN101080101&key=5c043b56de9f4371b0c7f8bee8f5b75e";
Map<String, Object> params = new HashMap<>();
params.put("start_time", "20180824");
params.put("end_time", "20180827");
// RestTemplate restTemplate = new RestTemplate();//此处直接autowire即可,不用new
HttpEntity httpEntity = new HttpEntity(params, null);
ResponseEntity<String> request = restTemplate.postForEntity(url, httpEntity, String.class);
return request.getBody().toString();
}
使用restTemplate来访问https的更多相关文章
- How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)
How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...
- C# 访问https 未能创建 SSL/TLS 安全通道
C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 一般GetResponse可以直接访问https ...
- WebClient 访问https
解决SSH证书问题: webClient.getOptions().setUseInsecureSSL(true);//解决ssh证书访问https的问题
- AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?
背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...
- 一招解决IE7无法访问https网页
很多人都遇到过这种情况: 自己的IE访问不了https的网页了,如果你百度的话,有人会告诉你注册一堆的dll文件,或者更改IE设置啦什么的.上午,我也遇到这个问题,这些方法都不管用.请教了高手,将方法 ...
- 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)
最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题, 用 echo phpinfo() ; 查看, 支持op ...
- curl+个人证书(又叫客户端证书)访问https站点
摘自http://blog.csdn.net/chary8088/article/details/22990741 curl+个人证书(又叫客户端证书)访问https站点 目前,大公司的OA管理系统( ...
- 转 c#代码访问https服务器以及https的webservice
最近公司做到WebService项目,但是要通过Https调用,自己在网上搜了半天,终于实现了服务端的Https,但是一直没有找到客户端如何实现,今天终于看到这篇文章,随手记录下来. 具体代码如下: ...
- 解决python2.7.9以下版本requests访问https的问题
在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...
随机推荐
- springboot 启动报错
有一个警告 :** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the d ...
- Vue项目使用bootstrap
①npm install boostrap@4.0.0 --save @4.0.0为版本号 ②在项目的main.js中添加 import 'bootstrap/dist/css/bootstrap.m ...
- c++有关构造函数、析构函数和类的组合的一个简单例子
来源链接 实验四(下) 代码 #include <iostream> using namespace std; enum CPU_Rank {P1 = 1, P2, P3, P4, P5, ...
- Java程序设计的第一次作业1
- java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context 错误
spring boot 项目启动报错:原因一般是注入了相同名字的service -- :: com.gxcards.mes.MainWwwWeb: logStartupProfileInfo INFO ...
- Eclipse手动添加web.xml
当创建web工程时,没有自动创建web.xml 这时候就需要手动添加web.xml 该怎么做呢 右键项目,点击java EE Tools 其中点击Genertate Deployment Descri ...
- list<实体类>互相嵌套和前台取值问题
list<实体类>嵌套list<实体类>,必须保证嵌套的实体类里面有这个list对象,把这个list<实体类>当做一个对象 这是需要解析的数据,并把这些数据封装成l ...
- Windows10 下安装 Apache2.4+PHP7.1+MySQL5.7
这个教程主要是分享如何快速组建WAMP开发环境,对于软件的详细配置,自行参考文档或搜索. Visual C++ Redistributable for Visual Studio 2015 下载地址: ...
- sql 2012 用户sa登陆不上
1.通过Windows身份验证进入服务器 2.点击“安全性”--“登录名”--“sa” 3.右击sa,点击“属性” 4.取消“强制实施密码策略(F)”勾选 5.对密码重新输入 6.点击确定,重新用SQ ...
- swig模板引擎汇总
1. Express中使用swig模板引擎 2.Swig 使用指南 3.jade to html online