HttpClient 通過域名訪問請求接口出現java.net.UnknownHostException解決方法

2016-08-16

    在項目中,有一個功能需要請求另外一個項目的接口來獲取數據。該項目接口都是通過域名請求訪問。每當調用到一定階段後都會出現未知域名,導緻請求數據失敗。以下是錯誤内容

java.net.UnknownHostException: XXXX.XXXX.com     at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)     at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)     at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)     at java.net.InetAddress.getAllByName0(InetAddress.java:1246)     at java.net.InetAddress.getAllByName(InetAddress.java:1162)     at java.net.InetAddress.getAllByName(InetAddress.java:1098)     at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)     at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111)     at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)     at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)     at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)     at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)     at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)     at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)     at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)     at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)     at com.moral.util.HttpTranfer.httpPostString(HttpTranfer.java:215)     at com.moral.job.util.TestTransfer.postTestSite(TestTransfer.java:59)     at com.moral.job.json.TestDaoJson.getTestInfo(TestDaoJson.java:127)     at com.moral.job.cache.TestCache.getTestInfo(TestCache.java:32)     at com.moral.job.service.impl.TestServiceImpl.getTestInfo(TestServiceImpl.java:56)     at com.moral.job.service.impl.JobServiceImpl.getJobDetailWithTest(JobServiceImpl.java:110)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)     at com.sun.proxy.$Proxy181.getJobDetailWithTest(Unknown Source)     at com.moral.job.view.JobController.editTest(JobController.java:406)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:606)     at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     at org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)     at java.lang.Thread.run(Thread.java:745)

               原因: 在host文件裡面主機名和本地循環地址沒有匹配到。 需要在hosts文件中添加上該域名的解析ip地址。

  • linux服務器

       vi  /etc/hosts        #添加以下一行内容        域名的解析ip    域名 

              執行service network restart 使之生效

  • windows服務器

             打開windws/system32/driver/etc/hosts,添加以下内容

       域名的解析ip    域名


http://m.juhua625748.cn|http://wap.juhua625748.cn|http://www.juhua625748.cn||http://juhua625748.cn