C#:HttpClient,服务器提交了协议冲突。 Section = ResponseStatusLine [英] C#: HttpClient, The server committed a protocol violation. Section=ResponseStatusLine

查看:631
本文介绍了C#:HttpClient,服务器提交了协议冲突。 Section = ResponseStatusLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HttpClient 类以与WPF应用程序中的Web服务进行通信。

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

当我在同一连接上连续发出GET请求时,一切正常。但是,当我在同一连接上发出连续的PUT / PATCH请求时,第一个请求正确执行,并且收到响应,但是第二个请求在请求中不包含正文,并且收到臭名昭著的错误:服务器提交了协议

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".

如果我在每个请求之后通过在 Connection:close 上添加手动关闭连接,则我的请求成功完成标头。此解决方案是一种错误的模式,性能无法适当扩展。

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.

下面是从发送的请求中获取的TCP流输出的列表的脱色版本:

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

Wireshark:跟踪TCP流输出

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

请注意,第二个PATCH缺少应该用来修补的对象。如果我更改了PATCH的顺序,则第二个PATCH仍然缺少其对象。

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.

对于我尝试过的一些已知解决方案,此错误似乎很常见。它们由此解决方案组成,其中涉及将useUnsafeHeaderParsing属性设置为 TRUE 并设置Keep-Alive Web.Config中的 FALSE 属性。我还尝试了如下所示设置这些属性的解决方案:

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;

这些解决方案均无效。应该注意的是,使用Http调试代理工具Fiddler捕获这些请求时,我没有收到任何错误。

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.

推荐答案

基本问题是PATCH响应中包含内容。响应的主体。发送204无内容时,请确保服务器未发送内容。

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.

这篇关于C#:HttpClient,服务器提交了协议冲突。 Section = ResponseStatusLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆