c# - 网络客户端错误(随机且非常烦人)

标签 c# stream webclient

WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri(url, UriKind.RelativeOrAbsolute));


void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{ 
  if (e.Error != null || e.Cancelled)
  {
    //randomly, this code gets hit and 
    //there is no inner exception - just says "an exception occurred during a webclient request"

代码中描述的错误是随机发生的,如果之后发出相同的 webclient 请求,它就会起作用。或者,如果发出新的资源请求,则出现错误的旧资源请求现在出现在新请求之前……

这很奇怪,这里的任何专家都知道为什么会这样吗?

编辑:错误信息

System.Net.WebException: An exception occurred during a WebClient request. ---> System.Exception ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component. at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.BrowserHttpWebRequest.<>c_DisplayClass5.b_4(Object sendState) at System.Net.AsyncHelper.<>c_DisplayClass2.b_0(Object sendState) --- End of inner exception stack trace --- at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result) --- End of inner exception stack trace ---

URL 是一个简单的字符串,计数不超过 200。

最佳答案

如果取消或生成错误,该代码将被命中。我怀疑你遇到了网络问题,所以我会启动 Fiddler并观察 HTTP 请求并确保您正确取回它们。

关于c# - 网络客户端错误(随机且非常烦人),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4162368/

相关文章:

c# - 抽象工厂接口(interface)和实际工厂应该在哪个库中定义?

c# - ASP.Net MVC 3 路由损坏

c# - 如何在 C# 中将 IEnumerable 转换为自定义类型?

ubuntu - 无法在 Wowza 流媒体引擎上发布流。服务器创建并立即销毁流

c# - 使用 .net WebClient 访问交叉签名的 SSL 服务

c# - 下载数千个网页的最有效方法

webclient - 如何在Windows Server 2012中安装Web客户端

c# - 我可以从 BackgroundWorker 上的 RunWorkerCompleted() 中调用 RunWorkerAsync() 吗?

stream - 如何在 Trident 中映射具有持久状态的元组?

python - mp3串流的python-proxy问题