首先-我是新来这个页面的,刚刚创建了我的帐户,因为我不能处理我要处理的问题。
我正在尝试从网站获取响应码-真正让我感兴趣的是我正在检查的页面是否存在,因此我正在寻找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协议和setRequestProperty假装成一个浏览器会有帮助,但它并没有。
有没有什么办法我可以修复这个问题,这样网站就会向我返回404错误?感谢您提前给予帮助!
发布于 2017-11-14 18:16:15
这取决于网站的响应。在任何浏览器中点击你的链接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);https://stackoverflow.com/questions/47263500
复制相似问题