网络代码有时会抛出 UnknownHostException

发布于 2024-09-03 09:39:33 字数 88 浏览 1 评论 0原文

我正在尝试从服务器获取数据。有时我的代码会由于 UnknownHostException 而失败。这是为什么?造成这个问题的原因是什么?

I am trying to data from a server. Sometimes my code fails due to an UnknownHostException. Why is that? What is the cause of this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女七分熟 2024-09-10 09:39:33

如果 DNS 服务器出现故障,则可能会发生这种情况。除了使 DNS 服务器更强大或寻找另一台 DNS 服务器之外,您还可以仅使用完整的 IP 地址而不是主机名。这样就不需要根据主机名查找 IP 地址。然而,我宁愿解决 DNS 问题,也更喜欢 DNS,因为 IP 地址可能会不时发生变化。

This may occur if a hiccup in DNS server has occurred. Apart from making the DNS server more robust or looking for another one, you can also just use the full IP address instead of the hostname. This way it doesn't need to lookup the IP address based on the hostname. However, I would rather fix the DNS issue and prefer the DNS since IP addresses may change from time to time.

许你一世情深 2024-09-10 09:39:33

UnknownHostException 表示指定的主机无法转换为 IP 地址。这很可能是您的 DNS 服务器的问题。

An UnknownHostException indicates the host specified couldn't be translated to an IP address. It could very well be a problem with your DNS server.

夜空下最亮的亮点 2024-09-10 09:39:33

如果 DNS 解析间歇性失败,请捕获异常并重试,直到获得名称解析。您只能控制您可以控制的内容...如果您无法控制/修复 DNS 服务器,请使您的应用程序足够强大以处理古怪的 DNS 服务器。

If the DNS resolution fails intermittently, catch the exception and try again until you get name resolution. You can only control, what you can control... And if you can't control/fix the DNS server, make your app robust enough to handle the quirky DNS server.

忆梦 2024-09-10 09:39:33

我使用了这篇文章的例子,在我的例子中是java版本。
jdk1.6 失败,但使用较新的版本则没有任何问题。

https://forums.freebsd.org /threads/java-problem-inetaddress-getlocalhost-is-not-working.26618/

i used the example of this post and in my case was the java version.
With jdk1.6 fails, but with a newer one it didn't have any problem.

https://forums.freebsd.org/threads/java-problem-inetaddress-getlocalhost-is-not-working.26618/

放赐 2024-09-10 09:39:33

我也在 Java 中看到零星的 UnknownHostExceptions,没有明显的原因。解决办法就是重试几次。这是 DocumentBuilder.parse 的包装器,它执行此操作:

static Document DocumentBuilder_parse(DocumentBuilder b, String uri) throws SAXException, IOException {
  UnknownHostException lastException = null;
  for (int tries = 0; tries < 2; tries++) {
    try {
      return b.parse(uri);
    } catch (UnknownHostException e) {
      lastException = e;
      System.out.println("Retrying because of: " + e);
      continue;
    }
  }
  throw lastException;
}

I too am seeing sporadic UnknownHostExceptions in Java for no apparent reason. The solution is just to retry a few times. Here is a wrapper for DocumentBuilder.parse that does this:

static Document DocumentBuilder_parse(DocumentBuilder b, String uri) throws SAXException, IOException {
  UnknownHostException lastException = null;
  for (int tries = 0; tries < 2; tries++) {
    try {
      return b.parse(uri);
    } catch (UnknownHostException e) {
      lastException = e;
      System.out.println("Retrying because of: " + e);
      continue;
    }
  }
  throw lastException;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文