我的代碼如下所示:正確的方式來阻止ReadAsStreamAsync
_req = new HttpRequestMessage(HttpMethod.Get, _uri);
_response = await _httpClient.SendAsync(_req, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
var rawResponse = new byte[_maxContentLength];
using (var responseStream = await _response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
int read;
int offset = 0;
do
{
read = await responseStream.ReadAsync(rawResponse, 0, rawResponse.Length).ConfigureAwait(false);
html += Encoding.UTF8.GetString(rawResponse, 0, read);
offset += read;
if (offset > _maxContentLength)
{
return Error("Too big");
}
} while (read != 0);
}
,是「返回錯誤()」,足以自由流和插座獲得下一個網址?
//編輯: 對於一些網頁await responseStream.ReadAsync
掛起並無法返回
您還需要在響應中添加using()。這應該釋放所有的資源。 –
_response被置於另一個地方,因爲它是一個整體變量 – ekapek