尽管KeepAlive等于true,为什么我不能多次使用HttpWebRequest?

时间:2017-02-20 19:03:59

标签: c# webrequest keep-alive

我正在尝试使用已经为源获取的HttpWebRequest来获取Content-Length和Resumeability。但是这样做会引发错误。我无法理解为什么会这样。

  

我的代码块

public void checkUrl(ref long size, ref bool resumeable)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "GET";
    req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
    req.Accept = "*/*";
    req.AllowAutoRedirect = true;
    req.MaximumAutomaticRedirections = 5;
    req.KeepAlive = true;
    req.Timeout = 10000;

    using (HttpWebResponse resp = req.GetHttpWebResponse())
       size = resp.ContentLength;


    req.AddRange(0, size / 2);
    using (HttpWebResponse resp = req.GetHttpWebResponse())
       resumeable = (resp.ContentLength == size / 2 + 1);

    req.Abort();
}

它会在第二个ObjectDisposedException语句中抛出using并说Cannot access a disposed object。我如何解决这种情况?

0 个答案:

没有答案