title: Simulating connection problems for .NET HttpWebRequest
body: <p>Are there ways to programmatically simulate connection problems (slow connection, response does not complete, connection gets dropped, etc.) when using the HttpWebRequest class?</p>
  
  <p>Thanks</p>
  
  <p>EDIT: To elaborate more, I need this for debugging but would want to turn it into a test eventually. I'm using the async methods BeginGetRequestStream, EndGetRequestStream, BeginGetResponse and EndGetResponse. I have wrapped them all in proper (I hope) Try Catch blocks which log the exceptions that happen.</p>
  
  <p>I know this works for some cases (e.g. when I pull out the network cable). But on some rare occasions (i.e. only when the website I'm requesting is slow) then my system crashes and I get this in the Event Log</p>
  
  <pre><code>Exception: System.Net.WebException
  
  Message: The request was aborted: The connection was closed unexpectedly.
  
  StackTrace:    at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
     at System.IO.Compression.DeflateStream.ReadCallback(IAsyncResult baseStreamResult)
     at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
     at System.Net.ContextAwareResult.CompleteCallback(Object state)
     at System.Threading.ExecutionContext.runTryCode(Object userData)
     at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Net.ContextAwareResult.Complete(IntPtr userToken)
     at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
     at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
     at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  </code></pre>
  
  <p>I am making an assumption it's from HttpWebRequest but then again all my code is wrapped in Try Catch blocks.</p>
  
  <p>Would mocks help in such a case?</p>