C# WebService 自定义Soap正文格式
接到一个webservice接口方案,对方请求soap格式如下(注意XML格式要求):
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.access.xx/"> <soapenv:Header/> <soapenv:Body> <ws:invoke> <service></service> <urid></urid> <pwd></pwd> <parameter><![CDATA[<rootxml></rootxml>]]></parameter> </ws:invoke> </soapenv:Body> </soapenv:Envelope>
使用宇宙最强IDE常规方式写出来的服务经过soapui开打后发现是这样的:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.access.xx/"> <soapenv:Header/> <soapenv:Body> <ws:invoke> <!--Optional:--> <ws:service>?</ws:service> <ws:urid>?</ws:urid> <ws:pwd>?</ws:pwd> <ws:parameter>?</ws:parameter> <ws:invoke> </soapenv:Body> </soapenv:Envelope>
对方请求后我这里无法接收数据。
百度后基本都是去怎么去请求这样的服务,或者是创建身份验证Header(https://www.cnblogs.com/springsnow/p/9428732.html)。
没办法开始翻阅官方接口文档找到解决方案:https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/k1y9z356(v=vs.100)
引用:using System.Web.Services.Description;using System.Web.Services.Protocols;
添加全局特性:[WebService(Namespace = "http://ws.access.xx/")]
方法代码(注意第二个特性个格式):
[WebMethod] [SoapRpcMethodAttribute("http://ws.access.xx/invoke", Use = SoapBindingUse.Literal)] public string invoke2(string service, string urid, string pwd, string parameter) { return ""; }
发布后使用soapui 测试发现模拟对方请求可以正常接收到信息。总共耗时三小时。