尝试从 FitNesse REST URI 读取响应时出现连接关闭错误

发布于 2024-08-11 03:16:03 字数 1734 浏览 2 评论 0原文

你遇到过这个问题吗?我运行的代码与 上一个问题,当处于 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;
    }

Have you run into this problem? I run code remarkably similar to that from a this previous question, When in nUnitTest mode and the URI includes "/?test&format=xml" the nUnit test fails with and IOException, "Unable to read data from the transport connection: The connection is closed."

However the Fiddler trace that was running at the time shows the very xml I expected.

I've recreated the request headers exactly (almost) as they are sent when sent through the browser.

Finally, if I leave off the "/?test&format=xml" from the URI, I get the html I would have otherwise expected.

SOURCE CODE:

    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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-08-18 03:16:03

错误消息“无法从传输连接读取数据:连接已关闭。”与您看到 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 状态,然后您无法重新打开它,直到它完全关闭关闭。如果是这种情况,请考虑使用其他端口或更改连接的生存时间。

The error message "Unable to read data from the transport connection: The connection is closed." doesn't really tie up with the fact you're seeing Fiddler getting a html response body back.

Check the StatusCode of the HttpWebResponse (should be 200 if ok), also wrap the request in try/catch block (example from 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);
}

If you're creating and disposing of the HttpWebRequest object quickly you might be getting the socket going into a time_wait state as it is shutting down, then you can't re-open it again until it has completely closed. If this is the case, look into using another port or changing the time the connection lives for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文