【问题标题】:HttpWebRequest GetRequestStream SystemException after several calls randomly几次随机调用后的HttpWebRequest GetRequestStream SystemException
【发布时间】:2018-01-12 10:50:18
【问题描述】:

我在 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 上的不同控制器)多次调用此方法,但突然间一切都卡住了。应用程序不再反应,当我在 Visual Studio 中按 PAUSE 时,我看到程序指针位于该位置

using (Stream requestStream = request.GetRequestStream())

有时 System.Net.Connection... 的 Timer.ring 中会引发 SystemException,但以防万一,应用程序不会继续运行,甚至不会将异常冒泡到下一个 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()

【问题讨论】:

    标签: c# exception httpwebrequest getresponsestream


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 您能在此处发布解决方案的相关部分吗?如果链接的内容更改或消失,此答案将毫无用处
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多