Question

I'm tring to capture a local POST requset and parse its data.
For testing only, the FiddlerCore should only response with the data it has parsed.
Here's the code of the FidlerCore encapsulation:

private void FiddlerApplication_BeforeRequest(Session oSession)
{
    if (oSession.hostname != "localhost") return;

    eventLog.WriteEntry("Handling local request...");
    oSession.bBufferResponse = true;
    oSession.utilCreateResponseAndBypassServer();
    oSession.oResponse.headers.HTTPResponseStatus = "200 Ok";
    oSession.oResponse["Content-Type"] = "text/html; charset=UTF-8";
    oSession.oResponse["Cache-Control"] = "private, max-age=0";

    string body = oSession.GetRequestBodyAsString();
    oSession.utilSetResponseBody(body);
}

Here's the code of the request sender:

const string postData = "This is a test that posts this string to a Web server.";

try
{
    WebRequest request = WebRequest.Create("http://localhost/?action=print");
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentLength = byteArray.Length;
    request.ContentType = "text/html";
    request.Method = "POST";

    using (Stream stream = request.GetRequestStream())
    {
        stream.Write(byteArray, 0, byteArray.Length);
    }

    using (WebResponse response = request.GetResponse())
    {
        txtResponse.Text = ((HttpWebResponse)response).StatusDescription;
        using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader streamReader = new StreamReader(stream))
            {
                string responseFromServer = streamReader.ReadToEnd();
                streamReader.Close();
                txtResponse.Text = responseFromServer;
            }
        }
    }
}
catch (Exception ex)
{
    txtResponse.Text = ex.Message;
}

I'm getting the following error:

The server committed a protocol violation. Section=ResponseStatusLine

What am I doing wrong?

Was it helpful?

Solution

Got it to work by changing:

WebRequest request = WebRequest.Create("http://localhost/?action=print");

to

WebRequest request = WebRequest.Create("http://localhost:8877/?action=print");

Calling this URL from a browser is intercepted by FiddlerCore correctly, without having to specify the port number. I did not think I should have inserted the listening port, since FiddlerCore should intercept all traffic, right?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top