spring boot restTemplagte access https can be accessed normally on window, Ali centos access timed out
the environmental background of the problems and what methods you have tried
related codes
restTemplate
@Bean
public RestTemplate restTemplate(OkHttpClient okHttpClient) {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory(okHttpClient));
}
@Bean
public OkHttpClient okHttpClient() {
HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.sslSocketFactory(getTrustedSSLSocketFactory())
.hostnameVerifier(DO_NOT_VERIFY);
return builder.build();
}
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
X509Certificate[] x509Certificates = new X509Certificate[0];
return x509Certificates;
}
@Override
public void checkClientTrusted(
X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
X509Certificate[] certs, String authType) {
}
}
};
private SSLSocketFactory getTrustedSSLSocketFactory() {
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
return sc.getSocketFactory();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
@Component
public class Run implements CommandLineRunner{
@Autowired
private ObjectMapper objectMapper;
@Autowired
private RestTemplate restTemplate;
@Override
public void run(String... args) throws Exception {
String currenty = "szhj_doge";
String str = this.restTemplate.getForObject("https://www.xunibi.biz/trade/index_json/market/szhj_doge?t=" + Math.random(), String.class);
JsonNode jsonNode = this.objectMapper.readTree(str);
JsonNode menu = jsonNode.get("menu");
JsonNode szhjDoge = menu.get(currenty);
String price = szhjDoge.get("price").asText();
System.out.println(price);
}
}
The execution on windows can get the price normally, but it cannot be obtained in Aliyun"s centos
.windows as shown in figure
centos
I hope someone can help me solve it.