当我在代码中查看下面的URL或下面的其他URL时,它显示正常。当我在浏览器中按F12时,我在网络选项卡中看不到任何异常,但是使用下面的代码我会得到响应代码403或400.当我在这里使用响应代码检查器时http://httpstatus.io/它将返回两个URL都有200响应。
我使用下面的代码获得http://psychsignal.com/的403。
URL u = new URL("http://www.nasdaqomxnordic.com/"); //returns 400 response code
//u.toURI(); //to check the syntax
HttpURLConnection huc = (HttpURLConnection)u.openConnection();
huc.setRequestMethod("GET");
//huc.setRequestMethod("HEAD");
huc.connect();
System.out.println(huc.getResponseCode());
谢谢,如果有人有任何想法!这实际上是我的第一篇文章!
答案 0 :(得分:1)
我的猜测是客户端的User-Agent存在一些限制。一些测试似乎支持我的理论:
如果我使用curl默认用户代理:
# curl -I -H "User-Agent: curl/7.35.0" "http://www.nasdaqomxnordic.com/"
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
Expires: 0
Connection: close
如果我使用被黑客攻击的标准浏览器代理字符串:
# curl -I -H "User-Agent: Mozilla/5.0" -0 "http://www.nasdaqomxnordic.com/"
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Content-Type: text/html;charset=UTF-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 22 Jul 2015 15:06:22 GMT
Connection: close
然后,如果我使用Java代理字符串(我猜你正在使用的是什么):
# curl -I -H "User-Agent: Java/1.6.0_26" "http://www.nasdaqomxnordic.com/"
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache
Pragma: no-cache
Expires: 0
Connection: close
只有“浏览器”用户代理才能通过。我会尝试调整代码来设置user agent string to something commonly found in a web browser。