0

My code looks like:

_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);
}

and is "return Error()" enough to free stream and sockets to get next url?

//edit: For some pages await responseStream.ReadAsync hangs and never returns

4

1 回答 1

0

尝试创建一个 CancellationTokenSource 并在您想要停止时调用 Cancel。我假设您无法在一开始就读取 Content-Length 标头,因为响应是块编码的?

于 2013-11-15T14:21:42.200 回答