使用 HttpWebRequest 类时,有没有办法以编程方式模拟连接问题(连接缓慢、响应未完成、连接断开等)?
谢谢
编辑:为了详细说明,我需要这个来调试,但最终想把它变成一个测试。我正在使用异步方法 BeginGetRequestStream、EndGetRequestStream、BeginGetResponse 和 EndGetResponse。我已经将它们全部包装在适当的(我希望)Try Catch 块中,记录发生的异常。
我知道这适用于某些情况(例如,当我拔出网络电缆时)。但在极少数情况下(即仅当我请求的网站速度很慢时)我的系统崩溃了,我在事件日志中得到了这个
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)
我假设它来自 HttpWebRequest 但我的所有代码再次包装在 Try Catch 块中。
在这种情况下,模拟会有所帮助吗?