0
我試圖做出部分GET請求,並期待206響應,但我仍然收到200 OK。我怎樣才能使它響應206?我如何在Java中創建http部分GET請求
這裏是我寫的代碼:
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
int start = Integer.parseInt(args[1]);
int end = Integer.parseInt(args[2]);
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Range", "bytes=" + start + "-" + end);
if (urlConn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL)
System.out.println ("File cannot be downloaded.");
else
{
String filepath = url.getPath();
String filename = filepath.substring (filepath.lastIndexOf('/') + 1);
BufferedInputStream in = new BufferedInputStream (urlConn.getInputStream());
BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (filename));
int temp;
while ((temp = in.read()) != -1)
{
out.write ((char)temp);
}
out.flush();
out.close();
System.out.println ("File downloaded successfully.");
}
我知道服務器不會忽略它。至少不應該。 – user1898452 2013-03-17 13:42:33
我有一個循環來檢查範圍是否包含整個文檔。那麼,至少我看到你說的代碼對我的目的是好的,但問題是關於別的。 – user1898452 2013-03-17 15:30:33