有没有办法获取ReadAsStringAsync() 方法的进度?我只是获取网站的 html 内容并进行解析。
是和不是。
HttpClient 不会从底层网络堆栈公开时间和进度信息,但您可以通过使用 HttpCompletionOption.ResponseHeadersRead、Content-Length 标头并读取响应来获取一些信息使用自己的StreamReader(当然是异步的)。
请注意,响应标头中的Content-Length 指的是解压缩前压缩内容的长度,而不是原始内容长度,这会使事情变得复杂,因为可能大多数 今天的网络服务器将提供带有gzip 压缩的HTML(和静态内容)(作为Content-Encoding 或Transfer-Encoding),因此Content-Length 标头不会告诉您解压缩内容的长度。不幸的是,虽然HttpClient 可以为您自动进行 GZip 解压缩,但它不会告诉您解压缩的内容长度是多少。
但是您可以仍然向您的方法的使用者报告某些类型的进度,请参见下面的示例。您应该使用 .NET 惯用的 IProgress<T> 接口来执行此操作,而不是自己滚动。
像这样:
private static readonly HttpClient _hc = new HttpClient()
{
DefaultRequestHeaders =
{
{ "User-Agent", "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" }
}
// NOTE: Automatic Decompression is not enabled in this HttpClient so that Content-Length can be safely used. But this will drastically slow down content downloads.
};
public static async Task<T> GetStartupAsync( IProgress<String> progress, string url = "http://")
{
progress.Report( "Now making HTTP request..." );
using( HttpResponseMessage response = await client.GetAsync( url, HttpCompletionOption.ResponseHeadersRead ) )
{
progress.Report( "Received HTTP response. Now reading response content..." );
Int64? responseLength = response.Content.Headers.ContentLength;
if( responseLength.HasValue )
{
using( Stream responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false) )
using( StreamReader rdr = new StreamReader( responseStream ) )
{
Int64 totalBytesRead = 0;
StringBuilder sb = new StringBuilder( capacity: responseLength.Value ); // Note that `capacity` is in 16-bit UTF-16 chars, but responseLength is in bytes, though assuming UTF-8 it evens-out.
Char[] charBuffer = new Char[4096];
while( true )
{
Int32 read = await rdr.ReadAsync( charBuffer ).ConfigureAwait(false);
sb.Append( charBuffer, 0, read );
if( read === 0 )
{
// Reached end.
progress.Report( "Finished reading response content." );
break;
}
else
{
progress.Report( String.Format( CultureInfo.CurrentCulture, "Read {0:N0} / {1:N0} chars (or bytes).", sb.Length, resposneLength.Value );
}
}
}
}
else
{
progress.Report( "No Content-Length header in response. Will read response until EOF." );
string result = await content.ReadAsStringAsync();
}
progress.Report( "Finished reading response content." );
}
注意事项:
- 一般而言,任何
async 方法或返回Task/Task<T> 的方法都应以Async 后缀命名,因此您的方法应命名为GetStartupAsync,而不是GetStartup。
- 除非您有可用的
IHttpClientFactory,否则您不应将HttpClient 包装在using 块中,因为这会导致系统资源耗尽,尤其是在服务器应用程序中。
- (原因很复杂,也可能因你的 .NET 实现而异(例如我相信 Xamarin 的
HttpClient 没有这个问题),但我不会在这里详细说明。
- 因此,您可以放心地忽略任何关于不处理您的
HttpClient 的代码分析警告。这是关于始终处置您创建或拥有的任何 IDisposable 对象的规则的少数例外情况之一。
- 由于
HttpClient 是线程安全的,并且这是static 方法,请考虑使用缓存的静态实例。
- 您也不需要将
HttpResponseMessage.Content 包装在using 块中,因为Content 对象归HttpResponseMessage 所有。