5

我目前正在使用 java 开发网络爬虫。我通过设置 tcp 连接并使用打印机手动发送 GET 请求。

我可以连接到大多数网站,例如 yahoo.com 或 cracked.com 并收到回复,但我无法连接到我的目标网站 -vinylengine.com。它总是会返回 302 错误。

我已经将我的发送请求与我的浏览器进行了比较,它们几乎相同。

我的标题:

GET / HTTP/1.1
Host: www.vinylengine.com

我的回复:

HTTP/1.1 302 Found
Date: Thu, 06 Jun 2013 19:27:00 GMT
Server: Apache
Location: http://www.nakedresource.com/
Cache-Control: max-age=1209600
Expires: Thu, 20 Jun 2013 19:27:00 GMT
Content-Length: 213
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.nakedresource.com/">here</a>.</p>
</body></html>

浏览器的标题:

GET http://www.vinylengine.com/ HTTP/1.1
Host: www.vinylengine.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __utma=72407316.18415374.1370488314.1370497873.1370543389.3; __utmz=72407316.1370488314.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); SESSaf8d12283bdbdc5f5bbfb2aef054db6d=1f0676e5cab0ba2c5a80e76ea0bd6f75; __utmc=72407316; has_js=1; __utmb=72407316
Connection: keep-alive
If-Modified-Since: Thu, 06 Jun 2013 18:02:53 GMT
If-None-Match: "2186d59ac297e0f1a43433fa61e8a94b"

代码:

public void sendRequest(String extensionString, String urlString)
{  
    try 
    {
        //BufferedReader inFromServer;
        //PrintWriter outToServer;
        //These 2 are initalized elsewhere

        outToServer.println("GET " + extensionString + " HTTP/1.1");
        outToServer.println("Host: " + urlString);

        outToServer.println("");
        outToServer.flush();

        String temp;
        while((temp=inFromServer.readLine()) != null) 
        {
            System.out.println(temp);
        }

        return;
    } 
    catch (Exception e) 
    {
        System.out.printf("sendRequest failed: %s",e);
        return;
    }
}

我尝试将主机名更改为裸资源.com,但是当我这样做时,我得到了裸资源.com 的页面源,而不是乙烯基引擎.com

4

3 回答 3

6

有问题的网站正在查看您的用户代理字符串(或者在您的情况下缺少)。

当您说您正在做与浏览器“几乎相同的事情”时……您是对的。计算机对这类事情有点挑剔。

如果您不提供User-Agent:标头,您将获得重定向。

> telnet www.vinylengine.com 80
正在尝试 67.225.154.112...
连接到vinylengine.com。
转义字符是 '^]'。
GET / HTTP/1.1
主机:www.vinylengine.com
接受:*/*

HTTP/1.1 302 Found
...

而如果您确实提供了一个,您将获得以下页面:

> telnet www.vinylengine.com 80
正在尝试 67.225.154.112...
连接到vinylengine.com。
转义字符是 '^]'。
GET / HTTP/1.1
主机:www.vinylengine.com
用户代理:curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
接受:*/*

HTTP/1.1 200 OK
...(页面)

这通常是因为站点向不同的浏览器提供不同版本的内容,这由User-Agent标题决定。显然,他们对“无用户代理”的回答是……平底船,您将被重定向到父站点根目录。

于 2013-06-06T19:47:50.570 回答
3
HttpURLConnection.setFollowRedirects(true);

如果您使用的是HttpURLConnection ,请使用上面的代码。

另请参阅显示 HTTP 重定向的示例

于 2013-06-06T19:43:56.693 回答
1

当您在浏览器中配置了代理但您的 JVM 不知道时,这可能是可能的。

尝试使用以下参数启动 JVM,看看它是否解决了问题:

-Dhttp.proxyHost=10.12.11.1 -Dhttp.proxyPort=8800
于 2013-06-06T19:37:39.640 回答