为什么这个java链接检查器代码不能编译?

发布于 2024-07-16 01:15:02 字数 1455 浏览 5 评论 0原文

我已经阅读了 SO 中发布的链接检查器问题。 如果还有更多此类问题而我错过了,我深表歉意。

我们需要以一种非常简单的方式和可编写脚本的方式在 网站 中找到损坏的链接,以便我们可以记录链接那些已经坏了。 我发现了一篇博客文章一些用java编写的代码可以完全满足我的需要,我的基础知识让我可以编译它,但每次都会出错。 我想也许这里有人可以指导我为什么代码无法编译。

这是代码:

import java.net.HttpURLConnection;
import java.net.URL;
class links
{

private static boolean isLive(String link) {
    HttpURLConnection urlConnection = null;
    try {
      URL url = new URL(link);
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("HEAD");
      urlConnection.connect();
      String redirectLink = urlConnection.getHeaderField("Location");
      if (redirectLink != null && !url.equals(redirectLink)) {
        return isLive(redirectLink);
      } else {
        return urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
      }
    } catch (Exception e) {
      return false;
    } finally {
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }
  }

  public static void main(String[] args) {
    System.out.println(isLive("http://www.fakelink.net"));
  }
}

感谢所有回复。 我把编译的代码放在这里,以供将来参考。

I already read the link checker question posted in SO. If there are more questions of this kind and I missed them, my apologies.

We need to find broken links in a website in a very easy way and scriptable way, so we can document the links that are broken. I found a blog post with some code written in java that will do exactly what I need, and my very basic knowledge let me compile it, but I get errors every time. I thought that maybe someone here might be able to direct me to why the code does not compile.

Here is the code:

import java.net.HttpURLConnection;
import java.net.URL;
class links
{

private static boolean isLive(String link) {
    HttpURLConnection urlConnection = null;
    try {
      URL url = new URL(link);
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("HEAD");
      urlConnection.connect();
      String redirectLink = urlConnection.getHeaderField("Location");
      if (redirectLink != null && !url.equals(redirectLink)) {
        return isLive(redirectLink);
      } else {
        return urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
      }
    } catch (Exception e) {
      return false;
    } finally {
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
    }
  }

  public static void main(String[] args) {
    System.out.println(isLive("http://www.fakelink.net"));
  }
}

Thanks to all that reply. I putting the code that compiles here, for future reference.

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

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

发布评论

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

评论(2

终陌 2024-07-23 01:15:02

您需要导入正确的包。 在这种情况下,您应该在文件顶部添加以下内容:

import java.net.HttpURLConnection;
import java.net.URL;

我还收到有关以下代码片段的编译器警告:

 && !url.equals(redirectLink)

It's attempts to Equate url (which is a URL) to redirectLink (which is a String) 。 在Java中,当发生这种情况时,它会自动比较字符串值,并且toString实际上给出了你想要的结果。 为了在作者代码中更精确,我将上面的代码更改为:

 && !url.toExternalForm().equals(redirectLink)

注意:在 netbeans 中,您只需右键单击并选择选项“修复导入”(或按 Ctrl + Shift + I),它将尝试找到正确的包您当前正在使用的并自动插入导入语句。

You need to have the proper packages imported. In this case, you should have the follow at the top of the file:

import java.net.HttpURLConnection;
import java.net.URL;

I'm also getting a compiler warning on the following piece of code:

 && !url.equals(redirectLink)

It's trying to equate url (which is a URL) to redirectLink (which is a String). In Java it will automatically compare the string value when this happens and the toString actually gives what you want. To be more precise in the authors code I would change the above code to:

 && !url.toExternalForm().equals(redirectLink)

Note: In netbeans, you can simply right click and select the option Fix Imports (or hit Ctrl + Shift + I) and it will try to find the correct packages that you're currently using and automatically insert the import statements.

骄兵必败 2024-07-23 01:15:02

该代码似乎是正确的。 可能是您没有导入所需的包。 如果您可以发布您遇到的编译器错误,这将会有所帮助。

The code seems correct. May be you have not imported the required Packages. It will help if you could post the compiler errors that you are getting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文