C# 使用HttpClient进行异步文件下载时检查错误
这个问题是一个后续问题 要使用HttpClient异步完成文件传输,需要将HttpCompletionOption.ResponseHeadersRead添加到SendAsync请求中。因此,当该调用完成时,您将能够通过添加对EnsureSuccessStatusCode的调用来确定请求和响应头是否一切正常。但是,此时数据可能仍在传输中 如何检测在返回标头之后但在数据传输完成之前发生的错误?所说的错误是如何表现出来的 下面是一些示例代码,问题点标记在第109行)上,并带有注释:“//****想在这里做更多的错误检查**”C# 使用HttpClient进行异步文件下载时检查错误,c#,.net,asynchronous,async-await,dotnet-httpclient,C#,.net,Asynchronous,Async Await,Dotnet Httpclient,这个问题是一个后续问题 要使用HttpClient异步完成文件传输,需要将HttpCompletionOption.ResponseHeadersRead添加到SendAsync请求中。因此,当该调用完成时,您将能够通过添加对EnsureSuccessStatusCode的调用来确定请求和响应头是否一切正常。但是,此时数据可能仍在传输中 如何检测在返回标头之后但在数据传输完成之前发生的错误?所说的错误是如何表现出来的 下面是一些示例代码,问题点标记在第109行)上,并带有注释:“//****想在
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Net.Http;
使用System.Threading.Tasks;
命名空间TestHttpClient2
{
班级计划
{
/*使用雅虎门户访问股票报价-执行异步操作*/
静态字符串baseUrl=”http://real-chart.finance.yahoo.com/";
静态字符串requestUrlFormat=“/table.csv?s={0}&d=0&e=1&f=2016&g=d&a=0&b=1&c=1901&ignore=.csv”;
静态void Main(字符串[]参数)
{
var activeTaskList=新列表();
字符串outputDirectory=“StockQuotes”;
如果(!Directory.Exists(outputDirectory))
{
CreateDirectory(outputDirectory);
}
while(true)
{
Console.WriteLine(“输入符号或[Enter]退出:”);
字符串符号=Console.ReadLine();
if(string.IsNullOrEmpty(符号))
{
打破
}
Task downloadTask=DownloadDataForStockAsync(输出目录,符号);
if(任务激活(下载任务))
{
//这是一个异步世界-在更新列表之前锁定它!
锁定(activeTaskList)
{
activeTaskList.Add(下载任务);
}
}
其他的
{
Console.WriteLine(“任务已完成?!”);
}
清理任务(activeTaskList);
}
控制台写入线(“清理”);
while(清理任务(activeTaskList))
{
Task.Delay(1.Wait();
}
}
私有静态bool清理任务(列表activeTaskList)
{
//反向循环以允许删除列表项
//这是一个异步世界-在更新列表之前锁定它!
锁定(activeTaskList)
{
对于(int i=activeTaskList.Count-1;i>=0;i--)
{
如果(!TaskIsActive(activeTaskList[i]))
{
activeTaskList.RemoveAt(i);
}
}
返回activeTaskList。计数>0;
}
}
专用静态布尔任务激活(任务任务)
{
返回任务!=null
&&task.Status!=TaskStatus.Cancelled
&&task.Status!=TaskStatus.Faulted
&&task.Status!=TaskStatus.RanToCompletion;
}
静态异步任务下载DataForStockAsync(字符串输出目录,字符串符号)
{
尝试
{
使用(var client=new HttpClient())
{
client.BaseAddress=新Uri(baseUrl);
client.Timeout=TimeSpan.frommins(5);
string requestUrl=string.Format(requestUrlFormat,符号);
var request=newhttprequestmessage(HttpMethod.Post,requestUrl);
var response=wait client.sendaync(请求,
HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
使用(var httpStream=await response.Content.ReadAsStreamAsync())
{
var timestampedName=FormatTimestampedString(符号,true);
var filePath=Path.Combine(outputDirectory,timestampedName+“.csv”);
使用(var fileStream=File.Create(filePath))
{
等待httpStream.CopyToAsync(fileStream);
}
}
//******要在此处执行更多错误检查吗*****
}
}
捕获(HttpRequestException-ex)
{
WriteLine(“线程上的异常:{0}:{1}\r\n”,
System.Threading.Thread.CurrentThread.ManagedThreadId,
例如,信息,
例如StackTrace);
}
捕获(例外情况除外)
{
WriteLine(“线程上的异常:{0}:{1}\r\n”,
System.Threading.Thread.CurrentThread.ManagedThreadId,
例如,信息,
例如StackTrace);
}
}
静态易失性字符串lastTimestampedString=string.Empty;
静态易失性字符串dummy=string.Empty;
静态字符串FormatTimestampedString(字符串消息,bool uniquify=false)
{
//这是一个异步世界-在使用共享资源之前锁定它!
锁(虚拟)
//锁(lastTimestampedString)
{
WriteLine(“线程内:{0:D2}lastTimestampedString:{1}”,
System.Threading.Thread.CurrentThread.ManagedThreadId,
lastTimestampedString);
字符串newTimestampedString;
while(true)
{
DateTime lastDateTime=DateTime.Now;
newTimestampedString=string.Format(
“{1:D4}{2:D2}{3:D2}{4:D2}{5:D2}{6:D2}{7:D3}{0}”,
消息
lastDateTime.Year,lastDateTime.Month,lastDateTime.Day,
lastDateTime.小时,lastDateTime.分钟,lastDateTime.秒,
lastDateTime。毫秒
);
如果(!uniquify)
{
打破
}
if(newTimestampedString!=lastTimestampedString)
{
打破
}
//Task.Delay(1.Wait();
};
lastTimestampedString=newTimestampedString;
WriteLine(“OUT-Thread:{0:D2}lastTimestampedString:{1}”,
System.Threading.Thread.CurrentThread.ManagedThreadId,
lastTimestampedString);
返回lastTimestampedStrin
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace TestHttpClient2
{
class Program
{
/* Use Yahoo portal to access quotes for stocks - perform asynchronous operations. */
static string baseUrl = "http://real-chart.finance.yahoo.com/";
static string requestUrlFormat = "/table.csv?s={0}&d=0&e=1&f=2016&g=d&a=0&b=1&c=1901&ignore=.csv";
static void Main(string[] args)
{
var activeTaskList = new List<Task>();
string outputDirectory = "StockQuotes";
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
while (true)
{
Console.WriteLine("Enter symbol or [ENTER] to exit:");
string symbol = Console.ReadLine();
if (string.IsNullOrEmpty(symbol))
{
break;
}
Task downloadTask = DownloadDataForStockAsync(outputDirectory, symbol);
if (TaskIsActive(downloadTask))
{
// This is an asynchronous world - lock the list before updating it!
lock (activeTaskList)
{
activeTaskList.Add(downloadTask);
}
}
else
{
Console.WriteLine("task completed already?!??!?");
}
CleanupTasks(activeTaskList);
}
Console.WriteLine("Cleaning up");
while (CleanupTasks(activeTaskList))
{
Task.Delay(1).Wait();
}
}
private static bool CleanupTasks(List<Task> activeTaskList)
{
// reverse loop to allow list item deletions
// This is an asynchronous world - lock the list before updating it!
lock (activeTaskList)
{
for (int i = activeTaskList.Count - 1; i >= 0; i--)
{
if (!TaskIsActive(activeTaskList[i]))
{
activeTaskList.RemoveAt(i);
}
}
return activeTaskList.Count > 0;
}
}
private static bool TaskIsActive(Task task)
{
return task != null
&& task.Status != TaskStatus.Canceled
&& task.Status != TaskStatus.Faulted
&& task.Status != TaskStatus.RanToCompletion;
}
static async Task DownloadDataForStockAsync(string outputDirectory, string symbol)
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUrl);
client.Timeout = TimeSpan.FromMinutes(5);
string requestUrl = string.Format(requestUrlFormat, symbol);
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
var response = await client.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var httpStream = await response.Content.ReadAsStreamAsync())
{
var timestampedName = FormatTimestampedString(symbol, true);
var filePath = Path.Combine(outputDirectory, timestampedName + ".csv");
using (var fileStream = File.Create(filePath))
{
await httpStream.CopyToAsync(fileStream);
}
}
// *****WANT TO DO MORE ERROR CHECKING HERE*****
}
}
catch (HttpRequestException ex)
{
Console.WriteLine("Exception on thread: {0}: {1}\r\n",
System.Threading.Thread.CurrentThread.ManagedThreadId,
ex.Message,
ex.StackTrace);
}
catch (Exception ex)
{
Console.WriteLine("Exception on thread: {0}: {1}\r\n",
System.Threading.Thread.CurrentThread.ManagedThreadId,
ex.Message,
ex.StackTrace);
}
}
static volatile string lastTimestampedString = string.Empty;
static volatile string dummy = string.Empty;
static string FormatTimestampedString(string message, bool uniquify = false)
{
// This is an asynchronous world - lock the shared resource before using it!
lock (dummy)
//lock (lastTimestampedString)
{
Console.WriteLine("IN - Thread: {0:D2} lastTimestampedString: {1}",
System.Threading.Thread.CurrentThread.ManagedThreadId,
lastTimestampedString);
string newTimestampedString;
while (true)
{
DateTime lastDateTime = DateTime.Now;
newTimestampedString = string.Format(
"{1:D4}_{2:D2}_{3:D2}_{4:D2}_{5:D2}_{6:D2}_{7:D3}_{0}",
message,
lastDateTime.Year, lastDateTime.Month, lastDateTime.Day,
lastDateTime.Hour, lastDateTime.Minute, lastDateTime.Second,
lastDateTime.Millisecond
);
if (!uniquify)
{
break;
}
if (newTimestampedString != lastTimestampedString)
{
break;
}
//Task.Delay(1).Wait();
};
lastTimestampedString = newTimestampedString;
Console.WriteLine("OUT - Thread: {0:D2} lastTimestampedString: {1}",
System.Threading.Thread.CurrentThread.ManagedThreadId,
lastTimestampedString);
return lastTimestampedString;
}
}
}
}
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
var response = await client.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using (var httpStream = await response.Content.ReadAsStreamAsync())
{
var timestampedName = FormatTimestampedString(symbol, true);
var filePath = Path.Combine(outputDirectory, timestampedName + ".csv");
using (var fileStream = File.Create(filePath))
{
await httpStream.CopyToAsync(fileStream);
}
}
try
{
using (var httpStream = await response.Content.ReadAsStreamAsync())
{
var timestampedName = FormatTimestampedString(symbol, true);
var filePath = Path.Combine(outputDirectory, timestampedName + ".csv");
using (var fileStream = File.Create(filePath))
{
await httpStream.CopyToAsync(fileStream);
}
}
}
catch (HttpRequestException e)
{
...
}
var httpStream = await response.Content.ReadAsStreamAsync();
public Task<Stream> ReadAsStreamAsync()
{
this.CheckDisposed();
TaskCompletionSource<Stream> tcs = new TaskCompletionSource<Stream>();
if (this.contentReadStream == null && this.IsBuffered)
{
this.contentReadStream = new MemoryStream(this.bufferedContent.GetBuffer(),
0, (int)this.bufferedContent.Length,
false, false);
}
if (this.contentReadStream != null)
{
tcs.TrySetResult(this.contentReadStream);
return tcs.Task;
}
this.CreateContentReadStreamAsync().ContinueWithStandard(delegate(Task<Stream> task)
{
if (!HttpUtilities.HandleFaultsAndCancelation<Stream>(task, tcs))
{
this.contentReadStream = task.Result;
tcs.TrySetResult(this.contentReadStream);
}
});
return tcs.Task;
}