2.16WebService 接口请求(SOAP)
WebService 是一种基于 SOA(面向服务架构)的应用程序,具有语言和平台无关性。它通过 XML 描述实现不同语言间的相互调用,并利用 HTTP 协议在 Internet 上进行网络应用间的交互。框架支持对 WebService 接口的请求,以下为示例代码:
WebService 是一种基于 SOA(面向服务架构)的应用程序,具有语言和平台无关性。它通过 XML 描述实现不同语言间的相互调用,并利用 HTTP 协议在 Internet 上进行网络应用间的交互。框架支持对 WebService 接口的请求,以下为示例代码:
SOAP 1.1#
var result = await httpRemoteService.PostAsStringAsync("http://您的主机地址/Share/DatabaseManager.asmx", builder => builder.SetSOAPAction("http://tempuri.org/GetDatabaseList") // 可配置自动追加双引号:addQuotes: true .SetXmlContent(""" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <Erp7SoapHeader xmlns="http://tempuri.org/"> <ID></ID> </Erp7SoapHeader> </soap:Header> <soap:Body> <GetDatabaseList xmlns="http://tempuri.org/" /> </soap:Body> </soap:Envelope> """, Encoding.UTF8));SOAP 1.2#
var result = await httpRemoteService.PostAsStringAsync("http://您的主机地址/Share/DatabaseManager.asmx", builder => builder.SetXmlContent(""" <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Header> <Erp7SoapHeader xmlns="http://tempuri.org/"> <ID></ID> </Erp7SoapHeader> </soap12:Header> <soap12:Body> <GetDatabaseList xmlns="http://tempuri.org/" /> </soap12:Body> </soap12:Envelope> """, Encoding.UTF8, "application/soap+xml"));在某些 WebService 接口返回的 XML 中,soap:Body 节点可能经过 Base64 编码和 GZip 压缩。此时,可通过以下代码进行解码和解压:
// 使用 XDocument 解析 XMLvar xDocument = XDocument.Parse(result!);// SOAP 1.1var bodyContent = xDocument.Descendants(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/")).FirstOrDefault()?.Value!;// SOAP 1.2// var bodyContent = xDocument.Descendants(XName.Get("Body", "http://www.w3.org/2003/05/soap-envelope")).FirstOrDefault()?.Value!;// Base64 解码var data = Convert.FromBase64String(bodyContent);// GZip 解压缩using var input = new MemoryStream(data);await using var gzip = new GZipStream(input, CompressionMode.Decompress);using var output = new MemoryStream();await gzip.CopyToAsync(output);// 获取实际内容var body = Encoding.UTF8.GetString(output.ToArray());