HttpWebRequest.GetResponse()挂起它被称为第二次 [英] HttpWebRequest.GetResponse() hangs the second time it is called

查看:156
本文介绍了HttpWebRequest.GetResponse()挂起它被称为第二次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过HTTP来获取一系列文件,使用HttpWebRequest的。第一个请求经过良好,但通过相同的代码的GetResponse第二次()挂起和超时。 Wireshark的表明,没有HTTP流量被发送的第二个请求,所以它会出现,这是一个API的问题。



经过一番调查,我发现它有做指定内容长度:如果我离开了这一点,那么代码工作正常。



我的代码是:

  HttpWebRequest的HttpWebRequest的= ConfigureRequest();使用(WebResponse类WebResponse类= httpWebRequest.GetResponse())
//在第二次迭代

我们从来没有超越这条线
{
HttpWebResponse httpWebResponse = WebResponse类为HttpWebResponse ;使用(流webResponseStream = httpWebResponse.GetResponseStream())
{
如果

(webResponseStream!= NULL)
{
//读取流
}
}

的StatusCode = httpWebResponse.StatusCode;
httpWebResponse.Close();
}

的症状似乎非常相似的这个问题并的这个问题,但在这两种情况下,给出的建议是处置WebResponse的,这我已经在做的。



修改为了应对格雷戈里,这里是ConfigureRequest():

 私人HttpWebRequest的ConfigureRequest()
{
串SURL = CreateURL(桶,键);
HttpWebRequest的HttpWebRequest的= WebRequest.Create(SURL)为HttpWebRequest的;

httpWebRequest.AllowWriteStreamBuffering = FALSE;
httpWebRequest.AllowAutoRedirect = TRUE;
httpWebRequest.UserAgent = this.m_sUserAgent;
httpWebRequest.Method =GET;
httpWebRequest.Timeout = this.m_iTimeout;

// ***注:此行留下了我原来的发帖的,竟然是
//关键
如果(m_contentLength大于0)
httpWebRequest.ContentLength = m_contentLength;

httpWebRequest.Headers.Add(StaticValues​​.Amazon_AlternativeDateHeader,时间戳);
httpWebRequest.Headers.Add(StaticValues​​.HttpRequestHeader_Authorization,StaticValues​​.Amazon_AWS ++ aWSAccessKeyId +:+签名);

返回HttpWebRequest的;
}

修改:原来我犯了红衣主教从我的问题,我还没有核实删除代码的罪是无关的问题。我已经删除以下行:

 如果(m_contentLength大于0)
httpWebRequest.ContentLength = m_contentLength;



,因为我认为内容长度绝不会为GET请求来指定。事实证明我错了。删除此行解决问题。



唯一的问题我现在已经是为什么呢?我的认为的被指定的内容长度是正确的,尽管它可能是由一个人的了。将指定内容长度过短,防止发生完全下载并导致悬空的连接?我本来预计,关闭()和/或Dispose()应当反正杀死连接。


解决方案

  httpWebRequest.Abort(); //你离开



将解决!


I'm trying to fetch a series of files via HTTP, using HttpWebRequest. The first request goes through fine, but the second time through the same code GetResponse() hangs and times out. WireShark shows that no HTTP traffic is being sent for the second request, so it would appear that it's an API problem.

After some investigation, I've discovered that it has to do with specifying the content-length: if I leave this out, then the code works fine.

My code is:

HttpWebRequest  httpWebRequest = ConfigureRequest();

using (WebResponse webResponse = httpWebRequest.GetResponse())
    // On the second iteration we never get beyond this line
{
    HttpWebResponse httpWebResponse = webResponse as HttpWebResponse;

    using (Stream webResponseStream = httpWebResponse.GetResponseStream())
    {
        if (webResponseStream != null)
        {
            // Read the stream
        }
    }

    statusCode = httpWebResponse.StatusCode;
    httpWebResponse.Close();
}

The symptoms seem very similar to this question and this question, but in both cases the advice given is to dispose of the WebResponse, which I'm already doing.

Edit In response to Gregory, here is ConfigureRequest():

private HttpWebRequest ConfigureRequest()
{
    string          sUrl            = CreateURL(bucket, key);
    HttpWebRequest  httpWebRequest  = WebRequest.Create(sUrl) as HttpWebRequest;

    httpWebRequest.AllowWriteStreamBuffering = false;
    httpWebRequest.AllowAutoRedirect = true;
    httpWebRequest.UserAgent = this.m_sUserAgent;
    httpWebRequest.Method = "GET";
    httpWebRequest.Timeout = this.m_iTimeout;

    // *** NB: This line was left out of my original posting, and turned out to be
    // crucial
    if (m_contentLength > 0)
        httpWebRequest.ContentLength = m_contentLength;

    httpWebRequest.Headers.Add(StaticValues.Amazon_AlternativeDateHeader, timestamp);
    httpWebRequest.Headers.Add(StaticValues.HttpRequestHeader_Authorization, StaticValues.Amazon_AWS + " " + aWSAccessKeyId + ":" + signature);

    return httpWebRequest;
}

Edit: It turns out I committed the cardinal sin of removing code from my question that I hadn't verified was unrelated to the problem. I had removed the following lines:

    if (m_contentLength > 0)
        httpWebRequest.ContentLength = m_contentLength;

because I thought that content-length would never be specified for a GET request. It turns out I was wrong. Removing this line fixes the problem.

The only question I now have is why? I think the content length that is specified is correct, though it's possible it's off by one. Would specifying a content length that is too short prevent the full download from taking place and cause the connection to be left open? I would have expected that Close() and / or Dispose() ought to kill the connection anyway.

解决方案

httpWebRequest.Abort(); // before you leave

Will resolve!

这篇关于HttpWebRequest.GetResponse()挂起它被称为第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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