简体   繁体   English

C# - 使用 WSHttpBinding 的 WS SOAP 1.2 请求中没有 POST 正文

[英]C# - No POST body in WS SOAP 1.2 request with WSHttpBinding

I am stuck with SOAP 1.2 request to ONVIF device.我被 SOAP 1.2 对 ONVIF 设备的请求所困扰。

My code is:我的代码是:

        var c = "http://192.168.31.12:5000/onvif/device_service";

        var wsBinding = new WSHttpBinding(SecurityMode.None);
        wsBinding.Name = "My WSHttpBinding";

        wsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        CustomBinding l_CustomBinding = new CustomBinding(wsBinding);
        MessageEncodingBindingElement l_EncodingElement =
             l_CustomBinding.Elements.Find<MessageEncodingBindingElement>();
        l_EncodingElement.MessageVersion = MessageVersion.Soap12;

        EndpointAddress endpointAddress = new EndpointAddress(c);

        fgdfsdfsdg.ServiceReference1.DeviceClient cl = new ServiceReference1.DeviceClient(l_CustomBinding, endpointAddress);
        fgdfsdfsdg.ServiceReference1.GetDeviceInformationRequest inValue = new fgdfsdfsdg.ServiceReference1.GetDeviceInformationRequest();
        var Q = cl.GetDeviceInformationAsync(inValue).Result;

What I see in Wireshark:我在 Wireshark 中看到的内容:

POST /onvif/device_service HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetDeviceInformation"
Host: 192.168.31.12:5000
Content-Length: 261
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

What expected by WS: WS 的期望:

POST /onvif/device_service HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver10/device/wsdl/GetDeviceInformation"
Host: 192.168.31.12:5000
Content-Length: 261
Accept-Encoding: gzip, deflate
Connection: Close

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetDeviceInformation xmlns="http://www.onvif.org/ver10/device/wsdl"/></s:Body></s:Envelope>

What should I add to my code to make C# send request body too, not only headers in HTTP POST request?我应该在我的代码中添加什么以使 C# 也发送请求正文,而不仅仅是 HTTP POST 请求中的标头?

The problem was in the header问题出在 header

Expect: 100-continue

which coming from HttpWebRequest .来自HttpWebRequest To disable this header I had to add要禁用此 header 我必须添加

System.Net.ServicePointManager.Expect100Continue = false;

before my code, ie my code now is在我的代码之前,即我现在的代码是

        var c = "http://192.168.31.12:5000/onvif/device_service";

        System.Net.ServicePointManager.Expect100Continue = false;

        var wsBinding = new WSHttpBinding(SecurityMode.None);
        wsBinding.Name = "My WSHttpBinding";

        wsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

        CustomBinding l_CustomBinding = new CustomBinding(wsBinding);
        MessageEncodingBindingElement l_EncodingElement =
             l_CustomBinding.Elements.Find<MessageEncodingBindingElement>();
        l_EncodingElement.MessageVersion = MessageVersion.Soap12;

The solution was found here https://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/ :在这里找到了解决方案https://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/

Apparently I'm not the only one to run into this annoying problem.显然,我不是唯一遇到这个烦人问题的人。 When using the HttpWebRequest to POST form data using HTTP 1.1, it ALWAYS adds the following HTTP header “Expect: 100-Continue”.当使用 HttpWebRequest 使用 HTTP 1.1 发布表单数据时,它总是添加以下 HTTP header “预期:继续1” Fixing the problem has proved to be quite elusive.解决这个问题已被证明是相当难以捉摸的。

According to the HTTP 1.1 protocol, when this header is sent, the form data is not sent with the initial request.根据 HTTP 1.1 协议,发送此 header 时,不会随初始请求发送表单数据。 Instead, this header is sent to the web server which responds with 100 (Continue) if implemented correctly.相反,这个 header 被发送到 web 服务器,如果实施正确,该服务器将响应 100(继续)。 However, not all web servers handle this correctly, including the server to which I am attempting to post data.但是,并非所有 web 服务器都能正确处理此问题,包括我尝试向其发布数据的服务器。 I sniffed the headers that Internet Explorer sends and noticed that it does not send this header, but my code does.我嗅探了 Internet Explorer 发送的标头,并注意到它没有发送此 header,但我的代码会发送。

....................... ....................................

UPDATE: Lance Olson points me to the solution in my comments section.更新:Lance Olson 在我的评论部分指出我的解决方案。 The System.Net.ServicePointManager class has a static property named Expect100Continue. System.Net.ServicePointManager class 有一个名为 Expect100Continue 的 static 属性。 Setting this to false will stop the header “Expect: 100-Continue” from being sent.将此设置为 false 将停止发送 header “期望:100-继续”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM