关于第二个电话的HttpWebRequest超时 [英] HttpWebRequest times out on second call

查看:336
本文介绍了关于第二个电话的HttpWebRequest超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下code超时运行它的第二个(和后续)的时间?

在code挂起,在

 (流objStream = request.GetResponse()。GetResponseStream())使用

,然后使引发WebException说,请求已超时。

我跟试过这样的的WebRequest 的HttpWebRequest

编辑:看来code在 request.GetResponse翻倒()

编辑:这篇文章表明,它可能是一个问题,气相色谱 - > <一个href=\"http://www.vbforums.com/showthread.php?t=610043\">http://www.vbforums.com/showthread.php?t=610043 - 按照这个职位的问题是,如果减轻提琴手是在后台打开

服务器是存在的,可用的请求。

 私人字符串GetQLMResponse(字符串URL)
    {
        HttpWebRequest的要求= WebRequest.Create(URL)为HttpWebRequest的;
        request.Credentials =新的NetworkCredential(Settings.Default.LicenseUser,Settings.Default.LicensePassword);
        request.KeepAlive = FALSE;
        request.Timeout = 5000;
        request.Proxy = NULL;        //读取流
        字符串responseString =的String.Empty;
        尝试
        {
            使用(VAR响应= request.GetResponse())
            {
                使用(流objStream = response.GetResponseStream())
                {
                    使用(StreamReader的objReader =新的StreamReader(objStream))
                    {
                        responseString = objReader.ReadToEnd();
                        objReader.Close();
                    }
                    objStream.Flush();
                    objStream.Close();
                }
                response.Close();
            }
        }
        赶上(引发WebException前)
        {
            抛出新LicenseServerUnavailableException();
        }
        最后
        {
            request.Abort();
            请求= NULL;
            所以GC.Collect();
        }
        返回responseString;
    }

抛出该异常引发WebException是:


  

{操作超时}
      [System.Net.WebException]:{操作超时}
      数据:{} System.Collections.ListDictionaryInternal
      HELPLINK:空
      的InnerException:空
      消息:操作超时
      资料来源:系统
      堆栈跟踪:在System.Net.HttpWebRequest.GetResponse(个)\\ r \\ n在IQX.Licensing.License.GetQLMResponse(字符串URL)在C:\\用户\\ JD \\ SVN \\ JD \\产品\\开发\\ JAD.Licensing \\ JAD .Licensing \\ License.cs:行373
      TargetSite:{System.Net.WebResponse的GetResponse()}



更新:好,那么下面的code现在的作品。该服务点设置了超时是近4分钟。请求对象的更改 ServicePoint.ConnectionLeaseTimeout 表示该请求后,5000毫秒现在被破坏。感谢所有您的帮助,也对这些2页:


  1. http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx

  2. <一个href=\"http://msdn.microsoft.com/en-us/library/6hszazfz(v=VS.80).aspx\">http://msdn.microsoft.com/en-us/library/6hszazfz(v=VS.80).aspx

     私人字符串GetQLMResponse(字符串URL)
    {
        HttpWebRequest的要求= WebRequest.Create(URL)为HttpWebRequest的;
        request.Credentials =新的NetworkCredential(Settings.Default.LicenseUser,Settings.Default.LicensePassword);
        request.KeepAlive = FALSE;
        request.Timeout = 5000;
        request.Proxy = NULL;    request.ServicePoint.ConnectionLeaseTimeout = 5000;
        request.ServicePoint.MaxIdleTime = 5000;    //读取流
        字符串responseString =的String.Empty;
        尝试
        {
            使用(WebResponse类响应= request.GetResponse())
            {
                使用(流objStream = response.GetResponseStream())
                {
                    使用(StreamReader的objReader =新的StreamReader(objStream))
                    {
                        responseString = objReader.ReadToEnd();
                        objReader.Close();
                    }
                    objStream.Flush();
                    objStream.Close();
                }
                response.Close();
            }
        }
        赶上(引发WebException前)
        {
            抛出新LicenseServerUnavailableException();
        }
        最后
        {
            request.Abort();
        }
        返回responseString;
    }



解决方案

在previous答案的高跟鞋,我想添加一些更多的东西。默认情况下的HttpWebRequest 只允许2个连接到同一个主机(这是HTTP 1.1的优先级),

是的,它可以被覆盖,不,我不会告诉你如何在这个问题上,你要问另外一个:)
我想你应该看看<一个href=\"http://stackoverflow.com/questions/4033159/c-httpwebrequest-times-out-after-two-server-500-errors\">this帖子。

我觉得你还是没有完全处理所有的资源与HttpWebRequest的连接,所以连接池进场,这就是问题。我不会试图争取每个服务器规则中的2个连接,除非你真的不得不这样做。

如上所述的海报之一,提琴手是在帮你有点帮倒忙在这种情况下。

我要你抓后添加一个漂亮的最后{} 条款,并确保为上述职位的笔记,所有的流被刷新,关闭并请求引用对象被设置为null。

请让我们知道这会有所帮助。

Why does the following code Timeout the second (and subsequent) time it is run?

The code hangs at:

using (Stream objStream = request.GetResponse().GetResponseStream())

and then causes a WebException saying that the request has timed out.

I have tried this with a WebRequest and HttpWebRequest

Edit: It seems the code is falling over in request.GetResponse()

Edit: This post suggests it may be a GC issue --> http://www.vbforums.com/showthread.php?t=610043 - as per this post the issue is mitigated if Fiddler is open in the background.

The server is there and available for requests.

    private string GetQLMResponse(string URL)
    {
        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        request.Credentials = new NetworkCredential(Settings.Default.LicenseUser, Settings.Default.LicensePassword);
        request.KeepAlive = false;
        request.Timeout = 5000;
        request.Proxy = null;

        // Read stream
        string responseString = String.Empty;
        try
        {
            using (var response = request.GetResponse())
            {
                using (Stream objStream = response.GetResponseStream())
                {
                    using (StreamReader objReader = new StreamReader(objStream))
                    {
                        responseString = objReader.ReadToEnd();
                        objReader.Close();
                    }
                    objStream.Flush();
                    objStream.Close();
                }
                response.Close();
            }
        }
        catch (WebException ex)
        {
            throw new LicenseServerUnavailableException();
        }
        finally
        {
            request.Abort();
            request = null;
            GC.Collect();
        }
        return responseString;
    }

Thrown WebException is:

{"The operation has timed out"} [System.Net.WebException]: {"The operation has timed out"} Data: {System.Collections.ListDictionaryInternal} HelpLink: null InnerException: null Message: "The operation has timed out" Source: "System" StackTrace: " at System.Net.HttpWebRequest.GetResponse()\r\n at IQX.Licensing.License.GetQLMResponse(String URL) in C:\Users\jd\SVN\jd\Products\Development\JAD.Licensing\JAD.Licensing\License.cs:line 373" TargetSite: {System.Net.WebResponse GetResponse()}


Update: OK So the following code now works. The servicePoint was setting the timeout to be near 4 minutes. Changing ServicePoint.ConnectionLeaseTimeout on the request object means that the request is now destroyed after 5000ms. Thanks to all for your help and also to these 2 pages:

  1. http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx
  2. http://msdn.microsoft.com/en-us/library/6hszazfz(v=VS.80).aspx

    private string GetQLMResponse(string URL)
    {
        HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
        request.Credentials = new NetworkCredential(Settings.Default.LicenseUser, Settings.Default.LicensePassword);
        request.KeepAlive = false;
        request.Timeout = 5000;
        request.Proxy = null;
    
        request.ServicePoint.ConnectionLeaseTimeout = 5000;
        request.ServicePoint.MaxIdleTime = 5000;
    
        // Read stream
        string responseString = String.Empty;
        try
        {
            using (WebResponse response = request.GetResponse())
            {
                using (Stream objStream = response.GetResponseStream())
                {
                    using (StreamReader objReader = new StreamReader(objStream))
                    {
                        responseString = objReader.ReadToEnd();
                        objReader.Close();
                    }
                    objStream.Flush();
                    objStream.Close();
                }
                response.Close();
            }
        }
        catch (WebException ex)
        {
            throw new LicenseServerUnavailableException();
        }
        finally
        {
            request.Abort();
        }
        return responseString;
    }
    

解决方案

On the heels of the previous answers, I wanted to add a couple more things. By default HttpWebRequest allows only 2 connections to the same host (this is HTTP 1.1 "niceness"),

Yes, it can be overriden, no I won't tell you how in this question, you have to ask another one :) I think you ought to look at this post.

I think that you are still not quite disposing of all your resources connected with the HttpWebRequest, so the connection pooling comes into play and that's the problem. I wouldn't try to fight the 2 connections per server rule, unless you really have to.

As one of the posters above noted, Fiddler is doing you a bit of a disservice in this case.

I'd add a nice finally {} clause after your catch and make sure that as the above post notes, all streams are flushed, closed and references to the request object are set to null.

Please let us know if this helps.

这篇关于关于第二个电话的HttpWebRequest超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆