我目前正在使用 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