重用的HttpWebRequest? [英] Reuse a HttpWebRequest?

查看:196
本文介绍了重用的HttpWebRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用的HttpWebRequest?

这似乎是第3次请求到一个站点会导致操作超时,似乎像,所以我想知道如果我可以通过更改URL,并再次获得该请求重复使用HttpWebRequest的每创建一个新的连接。 code的问题是下面这code是检查是否存在一系列的URL。

 静态无效storeList(TextWriter的SW,串urlTemplate,诠释开始,诠释完)
    {
        的for(int i =启动; I<结束;我++)
        {
            VAR URL =的String.Format(urlTemplate,我);
            VAR REQ =(HttpWebRequest的)HttpWebRequest.Create(URL);
            {
                req.Method =HEAD;
                tryHttpWebRequest
                {
                    VAR RESP = req.GetResponse();
                    sw.WriteLine(ⅰ);
                }
                赶上(例外五)
                {
                }
            }
        }
        sw.Flush();
    }
 

解决方案

您应该没问题,如果你只需要调用关闭你的回应。你只能这么多的开放式连接,因此它失败的原因是因为它不能打开一个新的连接。

一旦你的反应完成后,你需要关闭它......不需要重用什么。

MSDN文章

  

您必须调用要么Stream.Close   或HttpWebResponse.Close方法   关闭反应并释放   连接重用。它不是   需要通话双方Stream.Close   和HttpWebResponse.Close,但这样做   所以不会导致错误。

Am i able to use a HttpWebRequest?

It seems like the 3rd request to a site causes a operation timed out and it seems like each creates a new connection so i want to know if i can reuse a HttpWebRequest by changing the url and getting the request again. Code in question is below and this code is to check if a range of urls exist.

    static void storeList(TextWriter sw, string urlTemplate, int start, int end)
    {
        for (int i = start; i < end; i++)
        {
            var url = string.Format(urlTemplate, i);
            var req = (HttpWebRequest)HttpWebRequest.Create(url);
            {
                req.Method = "HEAD";
                tryHttpWebRequest
                {
                    var resp = req.GetResponse();
                    sw.WriteLine(i);
                }
                catch (Exception e)
                {
                }
            }
        }
        sw.Flush();
    }

解决方案

You should be ok if you just call Close on your response. You are only allowed so many "open" connections, so the reason it is failing is because it can't open a new connection.

Once you are done with the response, you need to close it... no need to reuse anything.

From the MSDN article:

You must call either the Stream.Close or the HttpWebResponse.Close method to close the response and release the connection for reuse. It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error.

这篇关于重用的HttpWebRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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