【问题标题】:C# - No POST body in WS SOAP 1.2 request with WSHttpBindingC# - 使用 WSHttpBinding 的 WS SOAP 1.2 请求中没有 POST 正文
【发布时间】:2020-04-15 10:48:02
【问题描述】:

我遇到了对 ONVIF 设备的 SOAP 1.2 请求。

我的代码是:

        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;

我在 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

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>

我应该在我的代码中添加什么以使 C# 也发送请求正文,而不仅仅是 HTTP POST 请求中的标头?

【问题讨论】:

    标签: c# web-services wcf soap


    【解决方案1】:

    问题出在标题中

    Expect: 100-continue
    

    来自HttpWebRequest。要禁用此标头,我必须添加

    System.Net.ServicePointManager.Expect100Continue = false;
    

    在我的代码之前,即我现在的代码是

            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;
    

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

    显然,我不是唯一遇到这个烦人问题的人。 当使用 HttpWebRequest 使用 HTTP 1.1 POST 表单数据时,它 始终添加以下 HTTP 标头“期望:100-继续”。定影 事实证明,这个问题非常难以捉摸。

    根据 HTTP 1.1 协议,发送此标头时,形式为 初始请求不发送数据。相反,这个标题是 发送到响应 100(继续)的 Web 服务器,如果 正确实施。然而,并不是所有的 Web 服务器都能处理这个 正确,包括我尝试向其发布数据的服务器。 我嗅探了 Internet Explorer 发送的标头并注意到它 不发送此标头,但我的代码发送。

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

    更新:Lance Olson 在我的 cmets 部分指出了解决方案。 System.Net.ServicePointManager 类有一个名为的静态属性 期待100继续。将此设置为 false 将停止标题“期望: 100-继续”从被发送。

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多