ソケットを使用して Google 検索結果を読み取っているときに応答でこのエラーが発生するたびに、検索するたびに応答でこのエラーが返され、302 応答が返されることがありますが、今では 301 が返されます。それを行うには、Googleから結果を取得したいだけで、ここで立ち往生するたびに、それを整理する方法:
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;
}