http - 收到奇怪的 http 响应代码,但该网站实际上正在运行

标签 http http-headers

当我在代码中查看下面的 URL 或下面的其他 URL 时,它显示正常。当我在浏览器中按 F12 时,我在网络选项卡中没有看到任何异常,但是使用下面的代码我将获得响应代码 403 或 400。当我在这里使用响应代码检查器时 http://httpstatus.io/对于两个 URLS 都会返回 200 响应。

我得到一个 403 http://psychsignal.com/使用我下面的代码。

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());

如果有人有任何想法,谢谢!这实际上是我的第一篇文章!

最佳答案

我的猜测是对客户端的 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 .

关于http - 收到奇怪的 http 响应代码,但该网站实际上正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561940/

相关文章:

php - 不使用表单将文件上传到服务器?

java - 如何通过 Java 8 中的 FileSystem 类连接外部文件系统?

http - CURL 命令行 URL 参数

c - 如何从 GET HTTP 请求中仅获取消息正文?

http-headers - TAR文件的正确MIME媒体类型

PHP file_get_contents() : content truncated from 2147483648 to 2147483647 bytes

ios - 如何解决这个问题 "Ambiguous reference to member ' dataTask(with :completionHandler: )' "

http - 使用 GET 变量的 301 URL 重定向

xml - R : "subscript out of bounds" 中的数据混搭

javascript - 无法使用 Node 请求模块进行基本的 HTTP 身份验证