c# - 多次随机调用后 HttpWebRequest GetRequestStream SystemException

标签 c# exception httpwebrequest getresponsestream

我在 C# .NET 3.5 CompactFramework 中有一个程序,它从 C# WebService 请求数据:

    public SynchronisationResult<J> Get<J>(IEnumerable<DomainProxy> existingObjects, string controller, string parameters) where J : IdJsonObject)
{
            string existingObjectsJson = JsonConvert.SerializeObject(existingObjects);
            string url = GenerateUrl(controller, parameters);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "text/json";
            request.Method = "POST";
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Timeout = 60000;
            request.ContentLength = existingObjectsJson.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                using (var streamWriter = new StreamWriter(requestStream))
                {
                    streamWriter.Write(existingObjectsJson);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                requestStream.Close();
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                string responseData = "";
                using (var streamReader = new StreamReader(response.GetResponseStream()))
                {
                    responseData = streamReader.ReadToEnd();
                }
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = JsonConvert.DeserializeObject<SynchronisationResult<J>>(responseData);
                }
                else
                {
                    throw new Exception(responseData);
                }
                response.Close();
            }
}

我可以使用不同的参数(WebServer 上的不同 Controller )多次调用此方法,突然一切都卡住了。应用程序不再有反应,当我在 Visual Studio 中按 PAUSE 时,我在该位置看到了程序指针

using (Stream requestStream = request.GetRequestStream())

有时 SystemException 会在 System.Net.Connection 的 Timer.ring 中抛出...,尽管以防万一,应用程序不会继续运行,甚至不会将异常冒泡到下一个 catch-Block。这意味着,我必须重置它永远不会继续运行的设备。

我尝试了以下更改来解决问题:

  • request.KeepAlive = true/false
  • request.Pipelines = true/false
  • ServicePointManager.DefaultConnectionLimit = 1000;
  • request.AutomaticDecompression = DecompressionMethods.GZip 或无

没什么,例如,请求在 Postman 中工作正常。 奇怪的是,如果我在 for 循环中实现它,要求更新大约 200 个对象,它会更快地崩溃。如果我在单击按钮时实现请求方法并以大约 10 秒的频率单击它,它的工作时间会更长。我尝试在端口 888 上使用开发 IIS 后端,在端口 80 上使用生产机器,本地防火墙已关闭。没有特定的请求会失败,它可能是类型 A 或 B 或 C 的请求,...每次运行都是不同的。

谁能解释一下:

a) 为什么代码卡住不继续?

b) 为什么即使抛出异常代码也会卡住

c) 如何配置 ServicePointManager 或 Request 以使其正常工作

编辑:这是执行 request.GetRequestStream() 时有时会发生的异常:

at System.Threading.Timer.startTimer(UInt32 dueTime)
at System.Threading.Timer.Change(UInt32 dueTime, UInt32 period)
at System.Threading.Timer.Change(Int32 dueTime, Int32 period)
at System.Threading.ThreadPool.QueueUserWorkItem(WaitCallback callBack, Object state, Boolean IsHttpRequest)
at System.Net.Connection.actionSending()
at System.Net.Connection.changeState(ConnectionState state)
at System.Net.Connection.transitionRequestSent(Event e)
at System.Net.Connection.processEvent(Event e)
at System.Net.Connection.actionRequestSent()
at System.Net.Connection.changeState(ConnectionState state)
at System.Net.Connection.transitionIdle(Event e)
at System.Net.Connection.processEvent(Event e)
at System.Net.Connection.submitRequest(HttpWebRequest request)
at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connGroupName)
at System.Net.HttpWebRequest.SubmitRequest()
at System.Net.HttpWebRequest.finishGetRequestStream()
at System.Net.HttpWebRequest.GetRequestStream()

最佳答案

我也被这个问题卡住了几天。最后我发现,如果我在后台运行 Fiddler,request.GetRequestStream() 中不会抛出异常。这意味着这与 fiddler 正在处理的连接池有关。所以我做了一些研究,发现以下链接解决了我的问题:

https://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app-

此外,在请求完成后,请确保您也将其中止。我所做的是:

if (webrequest != null) webrequest.Abort();

对我来说,现在一切正常。

关于c# - 多次随机调用后 HttpWebRequest GetRequestStream SystemException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507806/

相关文章:

c# - 匹配 M/YYYY、MM/YYYY、M/YY 或 MM/YY 格式的正则表达式

c# - 无法从 C#.net 应用程序连接到 oracle 服务器

exception - 常见的 lisp 异常处理(条件和重启)

c# - WebRequest中如何模拟DNS名称解析失败?

c# - 使用身份验证 token 休息后调用

c# - Func<T, bool> 和 Predicate<T> 编译后不是一回事吗?

c# - GhostDoc 类型工具的替代工具

c# - 如何防止在 C# 中出现异常?

javascript - 无法在 React 中处理 net::ERR_CONNECTION_REFUSED

C#获取或设置cookies,使用cookies从网络下载内容