从 WebClient 获取响应 Headers
Get Response Headers From WebClient
我正在尝试使用 WebClient 上传文件,但似乎无法找到获得响应的方法 headers 文件上传后我正在使用 this Microsoft example得到 headers,但它只是 returns null。我的代码如下所示:
public void UploadPart(string filePath, string preSignedUrl)
{
WebClient wc = new();
wc.UploadProgressChanged += WebClientUploadProgressChanged;
wc.UploadFileCompleted += WebClientUploadCompleted;
wc.UploadFileAsync(new Uri(preSignedUrl), "PUT", filePath);
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = wc.ResponseHeaders;
System.Diagnostics.Debug.WriteLine(myWebHeaderCollection);
}
我可以确认 headers 存在,因为我能够在另一种方法中使用 HttpWebResponse 上传实现来获取它们。
方法应该这样写
public Task UploadPart(string filePath, string preSignedUrl)
{
WebClient wc = new();
wc.UploadProgressChanged += WebClientUploadProgressChanged;
wc.UploadFileCompleted += WebClientUploadCompleted;
await wc.UploadFileAsync(new Uri(preSignedUrl), "PUT", filePath);
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = wc.ResponseHeaders;
System.Diagnostics.Debug.WriteLine(myWebHeaderCollection);
}
将 void 替换为 Task。
在 UploadFileAsyn 中使用 await,因为它可能无需请求就可以完成代码跳转到下一行。
我正在尝试使用 WebClient 上传文件,但似乎无法找到获得响应的方法 headers 文件上传后我正在使用 this Microsoft example得到 headers,但它只是 returns null。我的代码如下所示:
public void UploadPart(string filePath, string preSignedUrl)
{
WebClient wc = new();
wc.UploadProgressChanged += WebClientUploadProgressChanged;
wc.UploadFileCompleted += WebClientUploadCompleted;
wc.UploadFileAsync(new Uri(preSignedUrl), "PUT", filePath);
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = wc.ResponseHeaders;
System.Diagnostics.Debug.WriteLine(myWebHeaderCollection);
}
我可以确认 headers 存在,因为我能够在另一种方法中使用 HttpWebResponse 上传实现来获取它们。
方法应该这样写
public Task UploadPart(string filePath, string preSignedUrl)
{
WebClient wc = new();
wc.UploadProgressChanged += WebClientUploadProgressChanged;
wc.UploadFileCompleted += WebClientUploadCompleted;
await wc.UploadFileAsync(new Uri(preSignedUrl), "PUT", filePath);
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
WebHeaderCollection myWebHeaderCollection = wc.ResponseHeaders;
System.Diagnostics.Debug.WriteLine(myWebHeaderCollection);
}
将 void 替换为 Task。 在 UploadFileAsyn 中使用 await,因为它可能无需请求就可以完成代码跳转到下一行。