user1898452
user1898452

Reputation: 1

How can I make a http partial GET request in Java

I'm trying to make a partial GET request and I expect a 206 response, but I'm still getting 200 OK. How can I make it responded with a 206?

Here is the code I wrote:

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

Upvotes: 0

Views: 1149

Answers (1)

Stephen C
Stephen C

Reputation: 718788

How can I make it responded with a 206?

You can't force a server to respect your Range headers. The HTTP 1.1 spec says:

"A server MAY ignore the Range header."


You comment:

I know the server does not ignore it. At least shouldn't

Well apparently, the server >>IS<< ignoring the header. Or alternatively, the range that you are requesting encompasses the entire document.

Upvotes: 1

Related Questions