C#-在具有WSHttpBinding的WS-SOAP 1.2请求中没有POST主体



我被SOAP 1.2对ONVIF设备的请求卡住了。

我的代码是:

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#也发送请求主体,而不仅仅是HTTPPOST请求中的头?

问题出现在标头中

Expect: 100-continue

其来自CCD_ 1。为了禁用这个标题,我不得不添加

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使用HTTP1.1对表单数据进行POST时ALWAYS添加以下HTTP标头"Expect:100 Continue"。固定事实证明,这个问题很难解决。

根据HTTP 1.1协议,当发送此标头时数据不与初始请求一起发送。相反,此标头是发送到web服务器,如果正确执行。但是,并不是所有的web服务器都能处理此问题正确,包括我试图将数据发布到的服务器。我嗅了嗅Internet Explorer发送的标题,注意到它不发送此标头,但我的代码发送了。

更新:兰斯·奥尔森在我的评论部分向我指出了解决方案。System.Net.ServicePointManager类具有名为的静态属性预期100继续。将其设置为false将停止标头"Expect:100继续"。

最新更新