views:

118

answers:

0

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