Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: HttpClient, The server committed a protocol violation. Section=ResponseStatusLine

I'm using the HttpClient class to communicate to a web service in my WPF application.

When I make consecutive GET requests on the same connection, everything works fine. However, when I make consecutive PUT/PATCH requests on the same connection, the first request executes accurately and I receive a response but the second request does not include the body in the request and I receive the infamous error of "The server committed a protocol violation. Section=ResponseStatusLine".

My requests do complete successfully if I manually close the connection after every request by adding Connection: close to the header. This "solution" is a bad pattern and performance will not scale appropriately.

Below is a debranded version of a list of my TCP Stream Output from the requests being sent:

Wireshark: Follow TCP Stream Output

GET /domain/api/tenant/current/object?objectName=Lizbot HTTP/1.1
Accept: application/json

    HTTP/1.1 200 OK
    Content-Type: application/json; charset=utf-8
    Content-Length: 50
    {"Data":[{"Id":123,"ObjectName":"Lizbot","Date":null}],"Errors":[]}

PATCH /domain/api/tenant/current/object/123 HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Content-Length: 50
{"Id":123,"ObjectName":"Lizbot","Date":null}

    HTTP/1.1 204 No Content
    Content-Type: application/json; charset=utf-8
    {"Data":null,"Errors":[]}

PATCH /domain/api/tenant/current/object/123/otherObject HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8

    HTTP/1.1 400 Bad Request</b>
    Content-Type: text/html; charset=us-ascii
    Connection: close
    Content-Length: 311

Notice that the second PATCH is missing the object it's supposed to patch with. If I change the order of the PATCHing, the second PATCH is still missing its object.

This error appears to be common with a few known solutions which I have tried. They consist of this solution which involves setting the useUnsafeHeaderParsing property to TRUE and setting the Keep-Alive property to FALSE in the Web.Config. I also tried the solution of setting these properties in this manner shown below:

 ServicePointManager.DefaultConnectionLimit = 2;
 ServicePointManager.Expect100Continue = false;

None of these solutions worked. It should be noted that when using the Http Debugging proxy tool, Fiddler, to capture these requests, I don't receive any errors.

So what I am asking for is if anyone knows a good solution to alleviate this error so I can make multiple requests in a connection without losing the body of an update. If more details are needed, I am happy to supply them.

like image 272
Liz Miner Avatar asked Aug 28 '13 19:08

Liz Miner


2 Answers

After much debugging and reading, I realized I was trying to edit the Web.Config file of the WPF application instead of the app.config file!

So if you drop this code in the app.config file at the root of the configuration tag for a WPF application, it fixes the problem.

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>
like image 169
Liz Miner Avatar answered Oct 24 '22 19:10

Liz Miner


The underlying problem is that the PATCH response includes content within the body of the response. Ensure that the server does not send content when sending a 204 No Content.

like image 26
Daniel Avatar answered Oct 24 '22 18:10

Daniel