使用HttpClient设置范围头为*/*

本文关键字:范围 HttpClient 设置 使用 | 更新日期: 2023-09-27 18:16:20

我正在上传一个文件到Vimeo API,文档希望我通过发送一个空的放置请求来检查上传进度:

Content-Range: bytes */*

他们的API然后回复一个范围头,表明文件上传了多少。(https://developer.vimeo.com/api/upload/videos verify-the-upload)

我有一个while循环运行,而上传正在进行中,但我不知道如何将标题添加到PUT请求。uploadClient是我上传文件的同一个HttpClient实例。

while(!requestTask.IsCompleted)
{
    var progressRequest = await uploadClient.PutAsync(uploadLink.PathAndQuery, new StringContent(""));
    var progressResult = await progressRequest.Content.ReadAsStringAsync();
    Console.WriteLine("I will parse progress and output here");
    await Task.Delay(5000);
}

我如何在他们想要这个请求的格式添加范围头?

编辑:

我也试着像下面这样做,但我如何将*添加到范围内,它只接受长?

while(!requestTask.IsCompleted)
{
    var progressRequestMessage = new HttpRequestMessage(HttpMethod.Put, uploadLink.PathAndQuery);
    progressRequestMessage.Headers.Range.Ranges.Add(new RangeItemHeaderValue(*, *));
    var progressRequest = await uploadClient.SendAsync(progressRequestMessage);
    var progressResult = await progressRequest.Content.ReadAsStringAsync();
    Console.WriteLine("I will parse progress and output here");
    await Task.Delay(5000);
}
编辑:

我想我需要这样添加:

progressRequestMessage.Headers.Add("Range", "bytes */*");

这给了我一个无效的值异常,所以我认为可能有一种不同的方式来写bytes */*工作,但无法弄清楚。

使用HttpClient设置范围头为*/*

实际上添加标题很容易,只需将其添加到正文内容中即可。我找不到这个文档,所以很痛苦。

var bodyContent = new StringContent("");
bodyContent.Headers.Add("Content-Range", "bytes */*");
var progressRequest = await uploadClient.PutAsync(uploadLink.PathAndQuery, bodyContent);
var progressResult = await progressRequest.Content.ReadAsStringAsync();