HttpUrlConnection 返回 ResponseCode 404,但任何浏览器都可以随时访问该 URL

问题描述 投票:0回答:1

我需要打开一个到 URL 地址的连接并使用 StringBuffer 读取 html。但是,当我尝试建立连接时,出现文件未找到异常 404。当我在 Windows 或 Android 上的任何浏览器中写入此地址时,可以随时访问该地址。我不明白为什么会发生这种情况,或者是否有任何解决方法。

URL meteo = new URL("http://meteo.bg/bg/osnOpasni");
        HttpURLConnection connection = (HttpURLConnection) meteo.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Accept", "text/html, */*");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36");
        int responseCode = connection.getResponseCode();

如果我对任何其他 URL 使用相同的代码 - 它可以工作并且 ResponseCode 是 200。有什么我遗漏的吗???

java httpurlconnection http-response-codes
1个回答
0
投票

我在浏览器上使用 DevTools 尝试 de URL,使用 F12 网络分析首先出现 404 错误:

URL 似乎正在回答 404 错误:

您的代码没有错误,但网站中有错误。

© www.soinside.com 2019 - 2024. All rights reserved.