gpt4 book ai didi

java - 如何在 Java 中发出 http 部分 GET 请求

转载 作者:行者123 更新时间:2023-12-01 14:45:38 26 4
gpt4 key购买 nike

我正在尝试发出部分 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.");
}

最佳答案

How can I make it responded with a 206?

您无法强制服务器尊重您的 Range header 。 HTTP 1.1 规范 says :

"A server MAY ignore the Range header."

<小时/>

您评论:

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

显然,服务器 >>IS<< 忽略了 header 。或者,您请求的范围包含整个文档。

关于java - 如何在 Java 中发出 http 部分 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15461314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com