[英]Replace HttpWebRequest with HttpClient
由於這個原因, https://github.com/dotnet/corefx/issues/11873#issuecomment-470611032
我試圖用HttpClient替換HttpWebRequest
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.AllowWriteStreamBuffering = false;
using (var requestStream = await wr.GetRequestStreamAsync().ConfigureAwait(false))
{
await this.ZipFilesIntoStream(fileEntries, requestStream).ConfigureAwait(false);
}
而且我找不到任何有關如何使用HttpClient獲取流的指南。
using (var memoryStream = new MemoryStream())
using (var requestStream = await wr.GetRequestStreamAsync().ConfigureAwait(false))
{
await HttpClient.PostAsync(url, new StreamContent(memoryStream)
{
}).ConfigureAwait(false);
await this.ZipFilesIntoStream(fileEntries, requestStream).ConfigureAwait(false);
}
該代碼嘗試使用HttpClient,就像它是HttpWebRequest一樣。 HttpClient並不代表請求 。 這是一個執行不同HTTP方法的類。 應該在調用PostAsync
, GetAsync
或通用SendAsync
之前准備內容
我假設ZipFilesIntoStream
文件打包到流中。 由於MemoryStream
也可用,因此應將其用作存儲設備:
using (var memoryStream = new MemoryStream())
{
await ZipFilesIntoStream(fileEntries, memoryStream);
memoryStream.Position=0;
await client.PostAsync(url, new StreamContent(memoryStream));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.