首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用GetResponse()后HttpWebRequest中止

调用GetResponse()后HttpWebRequest中止
EN

Stack Overflow用户
提问于 2013-04-09 06:54:39
回答 1查看 2.1K关注 0票数 1

我的情况快把我逼疯了,而且网上没有任何真正有帮助的信息。

我正在自动化一个手动上传文件的过程。实际上,我在3年前写了这段代码,它工作了一年没有任何故障,现在它断断续续地失败了。在查看Fiddler流量中我的请求和刚刚从浏览器发出的请求时,我看到的唯一不同之处在于,在fiddler中,我会在自动调用期间看到这个红色圆圈,其中有一条线穿过它两次。

我的研究表明,这个图标意味着客户端中止了会话--我没有这么做。我为每个请求传递相同的CookieContainer对象。我不知道为什么会发生这种情况,但如果我在Fiddler中查看记录的行的属性,它会为我的自动请求显示以下内容:

代码语言:javascript
复制
SESSION STATE: Aborted.
...
X-ABORTED-WHEN: SendingResponse
...
== TIMING INFO ============
ClientConnected:    16:25:58.563
ClientBeginRequest: 16:25:58.566
GotRequestHeaders:  16:25:58.567
ClientDoneRequest:  16:25:58.567
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect: 0ms
HTTPS Handshake:    0ms
ServerConnected:    16:25:58.207
FiddlerBeginRequest:    16:25:58.567
ServerGotRequest:   16:25:58.567
ServerBeginResponse:    16:25:58.922
GotResponseHeaders: 16:25:58.922
ServerDoneResponse: 16:25:59.268
ClientBeginResponse:    16:25:59.268
ClientDoneResponse: 16:25:59.268

我在浏览器中得到的日志中的相同行如下所示:

代码语言:javascript
复制
SESSION STATE: Done.
...
ClientConnected:    10:33:09.347
ClientBeginRequest: 10:33:11.982
GotRequestHeaders:  10:33:11.982
ClientDoneRequest:  10:33:11.982
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect: 0ms
HTTPS Handshake:    0ms
ServerConnected:    10:33:08.050
FiddlerBeginRequest:    10:33:11.982
ServerGotRequest:   10:33:11.982
ServerBeginResponse:    10:33:12.337
GotResponseHeaders: 10:33:12.337
ServerDoneResponse: 10:33:12.511
ClientBeginResponse:    10:33:12.511
ClientDoneResponse: 10:33:12.514

所以“完成”vs.“已中止”。我从来没有对请求调用过Abort,也没有得到任何异常。下面是我的代码,其中发生了中止:

代码语言:javascript
复制
using (WebResponse httpResponse = httpRequest.GetResponse())
{
    if (!httpResponse.ResponseUri.AbsoluteUri.Equals(string.Format("{0}main.htm", url), StringComparison.CurrentCultureIgnoreCase))
    {
        throw new Exception("Log in failed. Check the Username and Password information in the Setting table.");
    }

    httpResponse.Close();
}

我试着去掉了"using“(即使它是推荐的)和"Close”(也是推荐的),但它仍然被中止。

我很感激

EN

回答 1

Stack Overflow用户

发布于 2013-05-16 04:55:34

我遇到了这个问题,我意识到如果响应流没有被读取(或访问),Fiddler中的请求将报告它已中止。对于我的用例,我不一定需要读取响应流,但我添加了一些代码来读取响应流(我没有缓冲或保存它),然后Fiddler报告请求已经完成。上面的代码没有读取响应流,所以我会尝试这样做。

代码语言:javascript
复制
        using (WebResponse httpResponse = httpRequest.GetResponse())
        {
            if (!httpResponse.ResponseUri.AbsoluteUri.Equals(string.Format("{0}main.htm", url), StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("Log in failed. Check the Username and Password information in the Setting table.");
            }

            var responseStream = httpResponse.GetResponseStream();
            if (null != responseStream)
            {
                var buffer = new byte[8192];
                while (responseStream.Read(buffer, 0, buffer.Length) > 0)
                {
                    // Do nothing here.
                }   
            }

            httpResponse.Close();
        }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15890452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档