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/