簡體   English   中英

如何在Java中發出http部分GET請求

[英]How can I make a http partial GET request in Java

我正在嘗試發出部分GET請求,但我希望得到206的響應,但是我仍然得到200 OK。 如何使它響應為206?

這是我寫的代碼:

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.");
}

如何使它響應為206?

您不能強迫服務器遵守您的Range標頭。 HTTP 1.1規范

服務器可以忽略Range標頭。


您評論:

我知道服務器不會忽略它。 至少不應該

顯然,服務器>> IS <<忽略標題。 或者,您要請求的范圍涵蓋整個文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM