Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查java中URL的有效性。这样就不会因为404错误而崩溃_Java_Validation_Url - Fatal编程技术网

检查java中URL的有效性。这样就不会因为404错误而崩溃

检查java中URL的有效性。这样就不会因为404错误而崩溃,java,validation,url,Java,Validation,Url,本质上,就像一辆防弹坦克,我希望我的程序能够吸收404个错误,并继续滚动,粉碎网络,留下死尸和死尸 我不断地发现这个错误: Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https://en.wikipedia.org/wiki/Hudson+Township+%28disambiguation%29 at org.jsoup.helper.Ht

本质上,就像一辆防弹坦克,我希望我的程序能够吸收404个错误,并继续滚动,粉碎网络,留下死尸和死尸

我不断地发现这个错误:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https://en.wikipedia.org/wiki/Hudson+Township+%28disambiguation%29
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:537)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)
at Q.Wikipedia_Disambig_Fetcher.all_possibilities(Wikipedia_Disambig_Fetcher.java:29)
at Q.Wikidata_Q_Reader.getQ(Wikidata_Q_Reader.java:54)
at Q.Wikipedia_Disambig_Fetcher.all_possibilities(Wikipedia_Disambig_Fetcher.java:38)
at Q.Wikidata_Q_Reader.getQ(Wikidata_Q_Reader.java:54)
at Q.Runner.main(Runner.java:35)
但我不明白为什么,因为我正在检查我是否有一个有效的URL,然后再导航到它。我的检查程序不正确怎么办

我试图研究关于这个主题的其他堆栈溢出问题,但它们不是很权威,而且我实现了许多来自和的解决方案,到目前为止没有任何效果

我正在使用apache commons URL验证程序,这是我最近使用的代码:

    //get it's normal wiki disambig page
    String URL_check = "https://en.wikipedia.org/wiki/" + associated_alias;

    UrlValidator urlValidator = new UrlValidator();

    if ( urlValidator.isValid( URL_check ) ) 
    {
       Document docx = Jsoup.connect( URL_check ).get();
        //this can handle the less structured ones. 


Status=404
错误意味着该位置没有页面。仅仅因为URL是有效的并不意味着那里有任何东西。验证器不能告诉你。唯一可以确定这一点的方法是获取它,并查看是否在执行操作时出现错误。

当下载的网页的状态代码返回除2xx以外的任何内容(如200或201等)时,
URLConnection
会抛出错误。而不是通过JTURN URL或字符串来解析文档,而是考虑传递一个包含网页的数据输入流。p> 使用
HttpURLConnection
类,我们可以尝试使用
getInputStream()
下载网页,并将其放置在
try/catch
块中,如果失败,则尝试通过
getErrorStream()
下载网页

考虑一下这段代码,它将下载您的wiki页面,即使它返回404

String URL_czech = "https://en.wikipedia.org/wiki/Hudson+Township+%28disambiguation%29";

URL wikidata_page = new URL(URL_czech);
HttpURLConnection wiki_connection = (HttpURLConnection)wikidata_page.openConnection();
InputStream wikiInputStream = null;

try {
    // try to connect and use the input stream
    wiki_connection.connect();
    wikiInputStream = wiki_connection.getInputStream();
} catch(IOException e) {
    // failed, try using the error stream
    wikiInputStream = wiki_connection.getErrorStream();
}
// parse the input stream using Jsoup
Jsoup.parse(wikiInputStream, null, wikidata_page.getProtocol()+"://"+wikidata_page.getHost()+"/");

您的url有效,响应为404未找到。阅读状态码404有没有一种快速的方法让我的程序忽略这些东西,这样它就不会一直崩溃?
try
/
catch
用于处理Java中的错误(包括可能忽略它们)。但是这里可能还有更多的事情,你必须深入研究。但是我的程序每次都会崩溃。我看到过这样的情况,我可以获取
或其他东西,但是没有关于如何实际实现它的好例子。Jsoup自己的文档似乎不太广泛。@s.Matthew_English在谷歌上搜索HTTP 404。它不是Java的东西,没有理由Java应该记录它,或者Jsoup。不,搜索“JSOUP404”。他希望具体了解如何使用他正在使用的Java客户机库处理404。“HTTP 404”是一个很好的背景信息,但是为什么您可能使用特定的客户端库获取它以及如何修复它涉及库特定的行为和配置,如用户代理字符串、重定向以下行为等等。(剧透者:我很确定这是他遇到的特定于库的行为,并且存在一些特定于Jsoup的SO问题来解决它。)哦,一个中间步骤,他可能已经完成了:复制URL并尝试在浏览器中使用另一个库或工具访问它。如果到处都坏了,可能是URL的问题。如果它只是在这个客户机中出现故障,这表明它是针对您正在使用的代理的更具体的东西,并且希望更多地寻找特定于工具的帮助。您的照片是否来自tycho相册
awake
?@s.Matthew\u English Yes太好了。那张专辑棒极了。还有一件让人震撼的事,那就是代码!它工作得很好。不错。这些天你还在听什么?也许你可以给我推荐一些东西。
String URL_czech = "https://en.wikipedia.org/wiki/Hudson+Township+%28disambiguation%29";

URL wikidata_page = new URL(URL_czech);
HttpURLConnection wiki_connection = (HttpURLConnection)wikidata_page.openConnection();
InputStream wikiInputStream = null;

try {
    // try to connect and use the input stream
    wiki_connection.connect();
    wikiInputStream = wiki_connection.getInputStream();
} catch(IOException e) {
    // failed, try using the error stream
    wikiInputStream = wiki_connection.getErrorStream();
}
// parse the input stream using Jsoup
Jsoup.parse(wikiInputStream, null, wikidata_page.getProtocol()+"://"+wikidata_page.getHost()+"/");