# 问题
http 请求发起后接收不到返回数据!!!【测试环境没出问题,发到正式环境就有问题】
项目中通过 restTemplate 发起请求:
1 | log.info("请求入参:{}",JSON.toJSONString(request));//打印日志1 |
- 打印日志 1 内容为:
http 请求入参:{data=[{ productStatus=10,skuCode=null}], messageId=ewpfpr1t6ey5r6qj0su0w1h6rt73hr,token=vgvU5EJKuZbuHii7WH6pTINp40ZRicaqLz4dq5P7L6pDzWir8EEGZhCKPucQjljsw69EHasEy+iJfdTofDg==}
打印日志 2 内容为:没有打印内容!!!
最后发现是因为测试环境中数据量较小,http 请求后,很快能得到相应,而正式环境数据量较大,没有及时得到响应,连接或者读取超时!!!
# 解决方式
# 第一种
添加 HttpsClientRequestFactory 类,并继承 SimpleClientHttpRequestFactory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57/**
* 兼容调Https接口
*/
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
protected void prepareConnection(HttpURLConnection connection, String httpMethod)
throws IOException {
if (connection instanceof HttpsURLConnection) {
prepareHttpsConnection((HttpsURLConnection) connection);
}
super.prepareConnection(connection, httpMethod);
}
private void prepareHttpsConnection(HttpsURLConnection connection) {
connection.setHostnameVerifier(new SkipHostnameVerifier());
try {
connection.setSSLSocketFactory(createSslSocketFactory());
}
catch (Exception ex) {
// Ignore
}
}
private SSLSocketFactory createSslSocketFactory() throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] { new SkipX509TrustManager() },
new SecureRandom());
return context.getSocketFactory();
}
private class SkipHostnameVerifier implements HostnameVerifier {
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
private static class SkipX509TrustManager implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}
}使用 restTemplate 发起请求前先设置连接和超时时间或者通过容器加载配置类然后设置超时时间
1
2
3
4
5
6//配置http请求的连接超时时间和读取超时时间
HttpsClientRequestFactory factory = new HttpsClientRequestFactory();
factory.setConnectTimeout(60 * 1000);
factory.setReadTimeout(5 * 60 * 1000);
RestTemplate restTemplate = new RestTemplate(factory);
BaseResult<QueryInventoryResult> result = restTemplate.postForObject(address.concat(inventoryUrl), request, Result.class);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class RestConfig {
//60 * 1000
private int connectTimeout;
//5 * 60 * 1000
private int readTimeout;
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
simpleClientHttpRequestFactory.setConnectTimeout(connectTimeout);
simpleClientHttpRequestFactory.setReadTimeout(readTimeout);
RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
return restTemplate;
}
# 第二种
1 |
|
# 第三种(基于第二种升级)
1 |
|