wpf C#:HttpClient,服务器违反了协议。部分 = ResponseStatusLine
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18496451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: HttpClient, The server committed a protocol violation. Section=ResponseStatusLine
提问by Liz Miner
I'm using the HttpClientclass to communicate to a web service in my WPF application.
我正在使用HttpClient类与我的 WPF 应用程序中的 Web 服务进行通信。
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".
当我在同一个连接上连续发出 GET 请求时,一切正常。但是,当我在同一个连接上发出连续的 PUT/PATCH 请求时,第一个请求执行准确并收到响应,但第二个请求不包含请求中的正文,我收到臭名昭著的错误“服务器提交了协议违规。部分= ResponseStatusLine”。
My requests do complete successfully if I manually close the connection after every request by adding Connection: closeto the header. This "solution" is a bad pattern and performance will not scale appropriately.
如果我在每个请求后通过添加Connection: close来手动关闭连接,我的请求会成功完成。这种“解决方案”是一种糟糕的模式,性能将无法适当扩展。
Below is a debranded version of a list of my TCP Stream Output from the requests being sent:
下面是我发送的请求的 TCP 流输出列表的去品牌化版本:
Wireshark: Follow TCP Stream Output
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
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.
请注意,第二个 PATCH 缺少它应该修补的对象。如果我更改 PATCH 的顺序,第二个 PATCH 仍然缺少它的对象。
This error appears to be common with a few known solutions which I have tried. They consist of this solutionwhich involves setting the useUnsafeHeaderParsing property to TRUEand setting the Keep-Alive property to FALSEin the Web.Config. I also tried the solution of setting these properties in this manner shown below:
这个错误似乎在我尝试过的一些已知解决方案中很常见。它们包含此解决方案,其中涉及在 Web.Config 中将 useUnsafeHeaderParsing 属性设置为TRUE并将 Keep-Alive 属性设置为FALSE。我还尝试了以如下所示的方式设置这些属性的解决方案:
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.
这些解决方案都没有奏效。需要注意的是,在使用Http Debugging 代理工具Fiddler 来捕获这些请求时,我没有收到任何错误。
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.
所以我要问的是,是否有人知道一个好的解决方案来缓解这个错误,这样我就可以在一个连接中发出多个请求而不会丢失更新的主体。如果需要更多详细信息,我很乐意提供。
采纳答案by Daniel
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.
潜在的问题是 PATCH 响应包含响应正文中的内容。确保服务器在发送 204 No Content 时不发送内容。
回答by Liz Miner
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!
经过大量调试和阅读后,我意识到我正在尝试编辑 WPF 应用程序的 Web.Config 文件而不是 app.config 文件!
So if you drop this code in the app.configfile at the root of the configuration tag for a WPF application, it fixes the problem.
因此,如果将此代码放在WPF 应用程序的配置标记根处的app.config文件中,它可以解决问题。
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
</system.net>

