未知主机异常 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31252915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:21:07  来源:igfitidea点击:

UnknownHostException java

javaexceptionunknown-host

提问by DataMiningEnthusiast

My program runs perfectly for sometime time, but after that I get an error

我的程序在一段时间内完美运行,但在那之后我收到一个错误

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:666)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.ReviewsSearch.getJsonResponse(ReviewsSearch.java:28)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:110)

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1167)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.productHierarchySearch.getHierarchy(productHierarchySearch.java:27)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:115)

Any idea, what can be the reason? Let me know if more information is required

任何想法,可能是什么原因?如果需要更多信息,请告诉我

edit: I tried ping command and I realized it is the internet connection. I am running the program in an ssh server and want to know how can I keep the code from stopping. I want it to wait for the connection.

编辑:我试过 ping 命令,我意识到这是互联网连接。我在 ssh 服务器中运行程序,想知道如何防止代码停止。我希望它等待连接。

采纳答案by Rob

Other answers address the particular exception you are seeing. However, you (probably) are really asking what to do about it.

其他答案解决了您看到的特定异常。但是,您(可能)确实在问该怎么做。

As you have encountered, in the real world things sometimes fail (and throws exceptions). In those cases in which the exception is expected to be temporary, you should be catching the exception and arranging to retry the operation. You may want to retry immediately, but more often it is better to wait a bit before trying again. The idea is to let whatever problem is happening get fixed (or fix itself) before trying again.

正如您所遇到的,在现实世界中,事情有时会失败(并抛出异常)。在预期异常是临时性的情况下,您应该捕获异常并安排重试操作。您可能想立即重试,但更多情况下,最好稍等片刻再试。这个想法是让正在发生的任何问题在再次尝试之前得到修复(或自我修复)。

It is often a good idea to log the exception (or at least a summary), in case operator intervention may be required to get things going again.

记录异常(或至少是摘要)通常是一个好主意,以防可能需要操作员干预才能使事情再次发生。

If you cannot proceed until that operation is successful, you probably want to retry in a loop. If you can work on other stuff, the retry might be implemented by putting the problem item at the back of the queue of work.

如果在该操作成功之前您无法继续,您可能希望在循环中重试。如果您可以处理其他事情,则可以通过将问题项放在工作队列的后面来实现重试。

For the former case:

对于前一种情况:

doWork("www.sears.com");

becomes

变成

while (true) {
    try {
        doWork("www.sears.com");
        break;
    } catch (UnknownHostException e) {
        logger.log(e.getMessage());
        Thread.sleep(10000);
    }
}

回答by Tharaka Devinda

It seems your server can't figure out the IP address of the server. Can you check whether DNS is working properly when this error pops up? Just open a command prompt window and try to ping that server www.sears.com

您的服务器似乎无法确定服务器的 IP 地址。当这个错误弹出时,你能检查DNS是否正常工作吗?只需打开一个命令提示符窗口并尝试 ping 该服务器 www.sears.com

回答by Anil Reddy Yarragonda

It emerges when you are trying to connect to a remote host using its host name, but the IP address of that host cannot be resolved.

当您尝试使用其主机名连接到远程主机,但无法解析该主机的 IP 地址时,它就会出现。

Form Docs:

表格文档:

Thrown to indicate that the IP address of a host could not be determined.

抛出表示无法确定主机的 IP 地址。

How to solve UnknownHostException?

如何解决 UnknownHostException?

UnknownHostExceptiondesignates a pretty straight forward problem. That the IP address of the remote host you are trying to reach cannot be resolved. So the solution to this is very simple. You should check the input of Socket (or any other method that throws an UnknownHostException), and validate that it is the intended one. If you are not whether you have the correct host name, you can launch a UNIX terminal and use the nslookup command (among others) to see if your DNS server can resolve the host name to an IP address successfully. Here is an example :

UnknownHostException指定一个非常直接的问题。您尝试访问的远程主机的 IP 地址无法解析。所以解决这个问题的方法很简单。您应该检查 Socket 的输入(或任何其他抛出 的方法UnknownHostException),并验证它是否是预期的。如果您不确定是否拥有正确的主机名,您可以启动 UNIX 终端并使用 nslookup 命令(以及其他命令)查看您的 DNS 服务器是否可以成功地将主机名解析为 IP 地址。这是一个例子:

nikos@nikos:~$ nslookup www.google.com
Server:     127.0.1.1
Address:    127.0.1.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 173.194.39.209
Name:   www.google.com
Address: 173.194.39.210
Name:   www.google.com
Address: 173.194.39.212
Name:   www.google.com
Address: 173.194.39.211
Name:   www.google.com
Address: 173.194.39.208

nikos@nikos:~$ 

If you are on Windows you can use the host command. If that doesn't work as expected then, you should check if the host name you have is correct and then try to refresh your DNS cache. If that doesn't work either, try to use a different DNS server, egGoogle Public DNS is a very good alternative.

如果您使用的是 Windows,则可以使用 host 命令。如果这没有按预期工作,那么您应该检查您拥有的主机名是否正确,然后尝试刷新您的 DNS 缓存。如果这也不起作用,请尝试使用不同的 DNS 服务器,例如Google 公共 DNS 是一个非常好的选择。

Reference:

参考:

http://examples.javacodegeeks.com/core-java/net/unknownhostexception/java-net-unknownhostexception-how-to-solve-unknownhostexception/

http://examples.javacodegeeks.com/core-java/net/unknownhostexception/java-net-unknownhostexception-how-to-solve-unknownhostexception/