栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中域名解析报错解决方案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中域名解析报错解决方案

ERROR o.s.s.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
org.springframework.web.client.ResourceAccessException: I/O error on GET request for “https://api.apishop.net/common/air/getCityPM25Detail”: api.apishop.net; nested exception is java.net.UnknownHostException: api.apishop.net
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673)

报错信息如下

创建 HttpClientUtil

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 HttpClientUtil {

	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 socketFactoryRegistry = RegistryBuilder.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;
	}
}

创建RestTemplateConfig

import datadockingpmdata.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


@Slf4j
@Configuration
public class RestTemplateConfig {

	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory){
		RestTemplate restTemplate = new RestTemplate(factory);
		return restTemplate;
	}

	@Bean(name = "httpsFactory")
	public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory()
	{
		try {
			CloseableHttpClient httpClient = HttpClientUtil.acceptsUntrustedCertsHttpClient();
			HttpComponentsClientHttpRequestFactory httpsFactory =
					new HttpComponentsClientHttpRequestFactory(httpClient);
			httpsFactory.setReadTimeout(40000);
			httpsFactory.setConnectTimeout(40000);
			return httpsFactory;
		}
		catch (Exception e ){
			log.info(e.getMessage());
			return  null;
		}
	}
}

在用restTemplate.getForEntity连接时用这个

ResponseEntity entity = restTemplateConfig.restTemplate(restTemplateConfig.httpComponentsClientHttpRequestFactory()).getForEntity(url, String.class);			

问题是本地可以解析,线上不能解析,奇了怪了,通过这个方法就解决啦
拾

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号