Getting error closing connection while trying to read response from FitNesse RIT URI

Have you encountered this problem? I am running code similar to the code of this previous question . When "/? Test & format = xml" is enabled in nUnitTest mode and in the URI, nUnit fails with IOException: "Unable to read data from transport connection: connection closed".

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

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

Finally, if I stay from "/? Test & format = xml" from the URI, I get html, which I would expect otherwise.

SOURCE OF 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;
    }
+3
1

" : ". , , Fiddler html.

StatusCode HttpWebResponse ( 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 , , . , .

0

Source: https://habr.com/ru/post/1722469/


All Articles