2

I have the following code to make a call that in turn returns xml:

private string Send(string url)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
    catch (WebException ex)
    {
        var _status = ex.Status.ToString();
        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            _status = ((HttpWebResponse)ex.Response).StatusCode.ToString();
        }
        Trace.WriteLine(_status.ToString());
    }

    return "error";
}

Most of the time (not always) I get

System.Net.WebException: The server committed a protocol violation.

Section=ResponseStatusLine at System.Net.HttpWebRequest.GetResponse()

exception thrown.

I have added this to my App.config directly in the <configuration> section:

<system.net>
  <settings>
    <httpWebRequest useUnsafeHeaderParsing="true"/>
  </settings>
</system.net>

But I continue to get the error.

Community
  • 1
  • 1
keeg
  • 3,990
  • 8
  • 49
  • 97
  • 4
    What kind is your target/url? http / https? What kind of server is at the destination, just any http server? You'd try to use Fiddler to check what are the communication steps. Also see http://stackoverflow.com/questions/2482715/the-server-committed-a-protocol-violation-section-responsestatusline-error – gerleim Oct 13 '16 at 14:54
  • for anyone stumbling over this. I had to set useUnsafeHeaderParsing to "true" on the client (caller) side to work around the issue between two of my services. Though this is not a permanent solution as of the mentioned security implications mentioned in the documentation https://learn.microsoft.com/en-us/dotnet/api/system.net.configuration.httpwebrequestelement.useunsafeheaderparsing?view=netframework-4.8.1 – rominator007 Dec 20 '22 at 07:54

2 Answers2

5

Set

request.KeepAlive = false;

http://www.webmonkeys.org.uk/2012/09/c-the-server-committed-a-protocol-violation-sectionresponsestatusline/#comment-60

Also, the useUnsafeHeaderParsing config value that you pasted has it set as false, when you should be setting it to true, if you're attempting to get past this issue.

Bruce Dunwiddie
  • 2,888
  • 1
  • 15
  • 20
  • thats a pasting error, its actually set to "true" in the code. I've corrected it. Thanks – keeg Oct 13 '16 at 15:14
  • I have a similar issue where an IIS application has "Anonymous Authentication" turned on. When using "Windows Authentication" the error disappears. Also when using "Anonymous Authentication" and Powershells Invoke-WebRequest with the parameter -DisableKeepalive (analogue to your answer) the error also disappears. Does anyone know what really causes the issue and why disabling KeepAlive works around it? – rominator007 Dec 20 '22 at 07:51
0
request.ServicePoint.Expect100Continue = true;