每次我在使用套接字读取谷歌搜索结果时在我的响应中收到这个错误,每次我搜索它都会给我这个错误响应,有时它给我 302 响应,现在它给我 301,我不知道是什么为此,我只想从谷歌获得结果,每次我被困在这里,如何解决:
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/search?q=java
Content-Type: text/html; charset=UTF-8
Date: Tue, 26 Feb 2013 10:57:46 GMT
Expires: Thu, 28 Mar 2013 10:57:46 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 232
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
301 Moved
The document has moved here
这是我的代码:
public String readGoogle(String keyword, int page) {
String content = "";
try {
Socket s = new Socket("google.com", 80);
PrintStream p = new PrintStream(s.getOutputStream());
p.print("GET /search?q=" + keyword + "&start=" + page
+ " HTTP/1.1\r\n");
p.print("User-Agent: Mozilla/4.0 "
+ "(compatible; MSIE 7.0; Windows NT 5.1)\r\n");
p.print("Connection: close\r\n\r\n");
InputStreamReader in = new InputStreamReader(s.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String line;
while ((line = buffer.readLine()) != null) {
content += line;
}
} catch (Exception e) {
e.printStackTrace();
}
return content;
}