检查HTTPS网站响应代码

时间:2017-11-13 11:41:43

标签: java url https http-status-code-404 http-status-code-503

开始 - 我是这个页面的新手,刚刚创建了我的帐户,因为我无法解决我遇到的问题。

我正在尝试从网站上获取响应代码 - 如果我检查的页面存在,我真正感兴趣的是所以我正在寻找404错误。我是这样做的:

public static void main(String[] args) throws IOException {
    URL url = new URL("https://bittrex.com/dffdgdfgdfgdfgfdgdf");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    int responseCode = con.getResponseCode();
    System.out.println(responseCode);

}

URL不存在,但它一直给我403(禁止访问)或503(服务不可用)响应代码,但该网站显然是404ed。我在某处读到,因为HTTPS protocole和setRequestProperty伪装成浏览器会有所帮助,但它没有。

无论如何我可以解决这个问题,以便网站向我返回404错误?我提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这取决于网站的响应。在任何浏览器中点击您的链接https://bittrex.com/dffdgdfgdfgdfgfdgdf,它将为您提供404.这就是您获得404的原因。

代码下方为您提供200个响应代码:

URL url = new URL("https://google.com");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    int responseCode = con.getResponseCode();
    System.out.println(responseCode);
相关问题