为什么这个java链接检查器代码不能编译?
我已经阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要导入正确的包。 在这种情况下,您应该在文件顶部添加以下内容:
我还收到有关以下代码片段的编译器警告:
It's attempts to Equate url (which is a URL) to redirectLink (which is a String) 。 在Java中,当发生这种情况时,它会自动比较字符串值,并且toString实际上给出了你想要的结果。 为了在作者代码中更精确,我将上面的代码更改为:
注意:在 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:
I'm also getting a compiler warning on the following piece of code:
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:
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.
该代码似乎是正确的。 可能是您没有导入所需的包。 如果您可以发布您遇到的编译器错误,这将会有所帮助。
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.