【问题标题】:Getting connection closed error when trying to read response from FitNesse REST URI尝试从 FitNesse REST URI 读取响应时出现连接关闭错误
【发布时间】:2012-02-23 18:43:46
【问题描述】:

你遇到过这个问题吗?我运行的代码与this previous question 中的代码非常相似,当处于 nUnitTest 模式且 URI 包含“/?test&format=xml”时,nUnit 测试失败并出现 IOException,“无法从传输连接读取数据:连接已关闭。”

但是,当时运行的 Fiddler 跟踪显示的正是我所期望的 xml。

我已经完全(几乎)重新创建了请求标头,就像它们通过浏览器发送时一样。

最后,如果我从 URI 中去掉“/?test&format=xml”,我会得到原本预期的 html。

源代码:

    public virtual bool Run()
    {
        var request = CreateRequest();
        var response = GetResponse(request);
        var responseString = ReadResponse(response);
        this.SetResults(responseString);
        return this.IsSuccessful;
    }

    protected internal virtual HttpWebRequest CreateRequest()
    {
        var address = TestConfig.Address;

        var request = (HttpWebRequest)WebRequest.Create(address);

        request.Accept = "*/*";
        request.UseDefaultCredentials = true;
        request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);

        return request;
    }

    protected internal virtual HttpWebResponse GetResponse(HttpWebRequest request)
    {
        var response = (HttpWebResponse) request.GetResponse();

        return response;
    }

    protected internal virtual string ReadResponse(HttpWebResponse response)
    {
        var stream = response.GetResponseStream();
        var responseString = ReadResponse(stream);

        stream.Close();
        response.Close();

        return responseString;
    }

    protected internal virtual string ReadResponse(Stream stream)
    {
        var reader = new StreamReader(stream);
        var responseString = reader.ReadToEnd();
        return responseString;
    }

【问题讨论】:

  • 我很感兴趣。源代码,请。 :-)
  • 添加源代码...
  • 服务器发送的 content-length 头是否与内容的实际长度匹配?
  • 不,返回的内容长度标头是-1。但是,当我使用 Fiddler 检查响应时,响应正文中有实际内容。
  • 也许设置 request.KeepAlive=false 会有所帮助...

标签: c# .net rest fitnesse


【解决方案1】:

错误消息“无法从传输连接读取数据:连接已关闭。”与您看到 Fiddler 获取 html 响应正文的事实并没有真正联系。

检查 HttpWebResponse 的 StatusCode(如果没问题,应该是 200),还将请求包装在 try/catch 块中(示例来自http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.statuscode(v=vs.80).aspx

try 
   {    
        // Creates an HttpWebRequest for the specified URL. 
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
        // Sends the HttpWebRequest and waits for a response.
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
        if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
           Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
                                myHttpWebResponse.StatusDescription);
        // Releases the resources of the response.
        myHttpWebResponse.Close(); 

    } 
catch(WebException e) 
   {
        Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status); 
   }
catch(Exception e)
{
    Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}

如果您正在快速创建和处置 HttpWebRequest 对象,您可能会在它关闭时让套接字进入 time_wait 状态,那么在它完全关闭之前您无法再次打开它。如果是这种情况,请考虑使用其他端口或更改连接的持续时间。

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 2021-04-29
    • 2015-05-01
    • 2021-03-26
    • 2018-05-01
    • 2013-05-02
    • 1970-01-01
    • 2019-12-03
    • 2012-10-04
    相关资源
    最近更新 更多