简体   繁体   中英

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

I am stuck with SOAP 1.2 request to ONVIF device.

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:

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:

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?

The problem was in the header

Expect: 100-continue

which coming from HttpWebRequest . To disable this header I had to add

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/ :

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”. 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. Instead, this header is sent to the web server which responds with 100 (Continue) if implemented correctly. However, not all web servers handle this correctly, including the server to which I am attempting to post data. I sniffed the headers that Internet Explorer sends and noticed that it does not send this header, but my code does.

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

UPDATE: Lance Olson points me to the solution in my comments section. The System.Net.ServicePointManager class has a static property named Expect100Continue. Setting this to false will stop the header “Expect: 100-Continue” from being sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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