每次我在使用套接字读取Google搜索结果的过程中遇到此错误时,每次搜索时都会给我这个错误,有时它会给我302响应,现在它给了我301 ,我不知道怎么用它做什么,我只是只想获得来自谷歌和每次我困在这里时间的结果,如何梳理出来:在java中的阅读结果
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;
}
请你解释一下,或者你可以给我提供示例代码来解决这个问题。 – ashishchaudharyjavaoracle 2013-02-26 11:46:33
如果你可以放松你的'Socket'类需求,你可以使用Apache中的HttpClient让你的生活更轻松。 – TheWhiteRabbit 2013-02-26 11:50:44