且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 C# 发送/接收 SOAP 请求和响应?

更新时间:2022-03-15 06:24:07

网址不同.

  • http://localhost/AccountSvc/DataInquiry.asmx

对比

  • /acctinqsvc/portfolioinquiry.asmx

首先解决这个问题,就像网络服务器无法解析您尝试 POST 到的 URL 一样,您甚至不会开始处理您的请求所描述的操作.

Resolve this issue first, as if the web server cannot resolve the URL you are attempting to POST to, you won't even begin to process the actions described by your request.

您应该只需要创建对 ASMX 根 URL 的 WebRequest,即:http://localhost/AccountSvc/DataInquiry.asmx,并在 SOAPAction 标头中指定所需的方法/操作.

You should only need to create the WebRequest to the ASMX root URL, ie: http://localhost/AccountSvc/DataInquiry.asmx, and specify the desired method/operation in the SOAPAction header.

SOAPAction 标头值不同.

  • http://localhost/AccountSvc/DataInquiry.asmx/+ methodName

对比

  • http://tempuri.org/GetMyName

您应该能够通过转到正确的 ASMX URL 并附加 ?wsdl

You should be able to determine the correct SOAPAction by going to the correct ASMX URL and appending ?wsdl

标签下应该有一个 标签与您尝试执行的操作相匹配,这似乎是成为 GetMyName.

There should be a <soap:operation> tag underneath the <wsdl:operation> tag that matches the operation you are attempting to execute, which appears to be GetMyName.

请求正文中没有包含您的 SOAP XML 的 XML 声明.

您在 HttpRequest 的 ContentType 中指定 text/xml 而没有字符集.也许这些默认为 us-ascii,但是如果您不指定它们,就无法确定!

You specify text/xml in the ContentType of your HttpRequest and no charset. Perhaps these default to us-ascii, but there's no telling if you aren't specifying them!

SoapUI 创建的 XML 包含一个 XML 声明,该声明指定了 utf-8 编码,它也匹配提供给 HTTP 请求的 Content-Type,即:text/xml;charset=utf-8

The SoapUI created XML includes an XML declaration that specifies an encoding of utf-8, which also matches the Content-Type provided to the HTTP request which is: text/xml; charset=utf-8

希望有帮助!