所以我做了一個可以下載4chan頁面的代碼。我得到原始的HTML頁面並解析它以滿足我的需要。下面的代碼工作正常,但它突然停止工作。當我運行它時,服務器不接受我的請求,它似乎在等待更多東西。但我知道HTTP請求如下當HTTP爲1.1時,HTTP GET請求不能在java中工作?
GET /ck HTTP/1.1
Host: boards.4chan.org
(extra new line)
如果我改變這種格式在任何情況下我復活「400壞請求」狀態代碼。但如果我將HTTP/1.1更改爲1.0,「200 ok」狀態下的服務器響應會顯示整個頁面。所以這使得我的錯誤是在主機中,因爲這在HTTP/1.1中變得強制。但我仍然無法弄清楚究竟需要改變什麼。
調用函數只是這一點,得到一個整板
downloadHTMLThread("ck", -1);
或特定線程你剛剛更改-1到該號碼。例如像下面的鏈接將有如下所示。
//http://boards.4chan.org/ck/res/3507158
//url.getDefaultPort() is 80
//url.getHost() is boards.4chan.org
//url.getFile() is /ck/res/3507158
downloadHTMLThread("ck", 3507158);
任何意見,將不勝感激,謝謝
public static final String BOARDS = "boards.4chan.org";
public static final String IMAGES = "images.4chan.org";
public static final String THUMBS = "thumbs.4chan.org";
public static final String RES = "/res/";
public static final String HTTP = "http://";
public static final String SLASH = "/";
public String downloadHTMLThread(String board, int thread) {
BufferedReader reader = null;
PrintWriter out = null;
Socket socket = null;
String str = null;
StringBuilder input = new StringBuilder();
try {
URL url = new URL(HTTP+BOARDS+SLASH+board+(thread==-1?SLASH:RES+thread));
socket = new Socket(url.getHost(), url.getDefaultPort());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("GET " +url.getFile()+ " HTTP/1.1");
out.println("HOST: " + url.getHost());
out.println();
long start = System.currentTimeMillis();
while ((str = reader.readLine()) != null) {
input.append(str).append("\r\n");
}
long end = System.currentTimeMillis();
System.out.println(input);
System.out.println("\nTime: " +(end-start)+ " milliseconds");
} catch (Exception ex) {
ex.printStackTrace();
input = null;
} finally {
if(reader!=null){
try {
reader.close();
} catch (IOException ioe) {
// nothing to see here
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException ioe) {
// nothing to see here
}
}
if(out!=null){
out.close();
}
}
return input==null? null: input.toString();
}
我有另一個與Apache協同工作的代碼,但我打算稍後對智能手機進行修改,所以我寧願不使用第三方庫。 – Shawn 2012-03-27 18:24:00
請聽詹姆斯,幫你一個忙:使用Apache HttpClient。如果通過智能手機,你的意思是Android,[Apache HttpClient是內置的](http://developer.android.com/reference/org/apache/http/package-summary.html)。 – 2012-03-27 18:31:02
我同意,但沒有什麼比編寫原始代碼更有效。我只是好奇,因爲這是越來越討厭 – Shawn 2012-03-27 18:32:43