HttpWebRequest中的System.ExecutionEngineException GetResponse C#

时间:2016-07-11 02:31:24

标签: c# exception httpwebrequest console-application httpwebresponse

我有一个控制台程序,其功能是使用URLs打开HttpWebRequest并在控制台窗口中显示结果。

我运行了几天(超过一周),直到今天它在此部分出现错误System.ExecutionEngineException时才出现问题:

HttpWebResponse response = (HttpWebResponse)request.GetResponse()

错误讯息:

System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>Clicker.exe</AppDomain><Exception><ExceptionType>System.ExecutionEngineException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Exception of type 'System.ExecutionEngineException' was thrown.</Message><StackTrace>   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
   at System.Net.ConnectStream.ProcessWriteCallDone(ConnectionReturnResult returnResult)
   at System.Net.HttpWebRequest.WriteCallDone(ConnectStream stream, ConnectionReturnResult returnResult)
   at System.Net.ConnectStream.CallDone(ConnectionReturnResult returnResult)
   at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
   at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   at System.Net.ConnectStream.CloseInternal(Boolean internalCall)
   at System.Net.HttpWebRequest.EndWriteHeaders_Part2()
   at System.Net.HttpWebRequest.EndWriteHeaders(Boolean async)
   at System.Net.HttpWebRequest.WriteHeadersCallback(WebExceptionStatus errorStatus, ConnectStream stream, Boolean async)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   at System.Net.HttpWebRequest.EndSubmitRequest()
   at System.Net.HttpWebRequest.SetRequestSubmitDone(ConnectStream submitStream)
   at System.Net.Connection.CompleteConnection(Boolean async, HttpWebRequest request)
   at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
   at System.Net.Connection.CompleteStartRequest(Boolean onSubmitThread, HttpWebRequest request, TriState needReConnect)
   at System.Net.Connection.SubmitRequest(HttpWebRequest request, Boolean forcedsubmit)
   at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName)
   at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
   at System.Net.HttpWebRequest.GetResponse()
   at Clicker.ClickerModule.OpenURL(CLKProxyModel proxy, CLKURLModel url)
   at Clicker.ClickerModule.&amp;lt;PrepareProxies&amp;gt;b__20_0(CLKProxyModel proxy)
   at System.Threading.Tasks.Parallel.&amp;lt;&amp;gt;c__DisplayClass31_0`2.&amp;lt;ForEachWorker&amp;gt;b__0(Int32 i)
   at System.Threading.Tasks.Parallel.&amp;lt;&amp;gt;c__DisplayClass17_0`1.&amp;lt;ForWorker&amp;gt;b__1()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.&amp;lt;&amp;gt;c__DisplayClass176_0.&amp;lt;ExecuteSelfReplicating&amp;gt;b__0(Object )
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp;amp; currentTaskSlot)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
   at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
</StackTrace><ExceptionString>System.ExecutionEngineException: Exception of type 'System.ExecutionEngineException' was thrown.</ExceptionString></Exception></TraceRecord>

OpenURL功能:

private bool OpenURL(CLKProxyModel proxy, CLKURLModel url) {
   if (!url.Link.StartsWith("http")) {
      url.Link = "http://" + url.Link;
   }

   Uri uri;
   if(!Uri.TryCreate(url.Link, UriKind.Absolute, out uri)) {
      _logger.Error("Invalid URI: The hostname could not be parsed.");
      return false;
   } else {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Link);
      WebProxy wProxy = new WebProxy(proxy.IP, proxy.Port.Value);
      wProxy.BypassProxyOnLocal = false;
      wProxy.UseDefaultCredentials = true;
      request.Proxy = wProxy;
      request.KeepAlive = false;
      request.UseDefaultCredentials = true;
      request.Timeout = TIMEOUT;
      request.ReadWriteTimeout = READWRITETIMEOUT;
      request.Method = "GET";
      request.Headers.Add("Accept-Language", "en-US");
      request.Accept = "text/html, application/xhtml+xml, */*";
      request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36";

      request.ServicePoint.ConnectionLeaseTimeout = CONNECTIONLEASETIMEOUT;
      request.ServicePoint.MaxIdleTime = MAXIDLETIME;
      request.ServicePoint.ConnectionLimit = numberOfProxy;

      try {
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
          string msg = string.Format(">>>>>>\nurl: {0}, proxy: {1}:{2}\n>> {3}\n>> {4}", url.Link, proxy.IP, proxy.Port.Value, response.StatusCode, response.Headers);
          Console.WriteLine(msg);
          return true;
        }
      } catch (WebException e) {
        request.Abort();

        string msg = string.Format("=======\nurl: {0}, proxy: {1}:{2}\n=== {3}\n=== {4}", url.Link, proxy.IP, proxy.Port.Value, e.Status, e.Message);
          Console.WriteLine(msg);
        return false;
      }
   }
}

OpenURL将像这样调用:

Parallel.ForEach(clkProxies, proxy => {
   foreach (CLKURLModel url in clkURLs) {
      OpenURL(proxy, url);
      int pauseTime = rand.Next(500, 2001);
      Thread.Sleep(pauseTime);
   }
});

为什么突然出现此错误?

0 个答案:

没有答案