【发布时间】:2020-06-15 13:50:58
【问题描述】:
我需要调用一个 SOAP 端点并且我正在使用 NET Core。内置的 WCF 功能似乎无法处理我需要它做的事情,因为我需要在正文中设置一个 SecurityToken 并且它没有该功能。我已经在 SO 中四处浏览,寻找潜在的解决方案,但还没有一个对我有用。这是我当前的代码:
public async Task<string> CreateSoapEnvelope()
{
string soapString = $@"
<soap:Envelope
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:wsa=""http://schemas.xmlsoap.org/ws/2004/08/addressing"">
<soap:Header>
<wsa:Action>actionAddress</wsa:Action>
<wsa:MessageID>uuid</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:To>address</wsa:To>
</soap:Header>
<soap:Body>
<Transaction>
<Input>
<Inquiry> !--Request Data Here --!
</Inquiry>
</Input>
<SecurityToken SepecialUserId=####### Token=STS TOKEN HERE/>
</Transaction>
</soap:Body>
</soap:Envelope>";
HttpResponseMessage response = await PostXmlRequest(Configuration.GetValue<string>("AppKeys:EndpointAddress"), soapString);
string content = await response.Content.ReadAsStringAsync();
return content;
}
public static async Task<HttpResponseMessage> PostXmlRequest(string baseUrl, string xmlString)
{
using (var httpClient = new HttpClient())
{
var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");
httpContent.Headers.Add("Transaction", "actionaddress");
return await httpClient.PostAsync(baseUrl, httpContent);
}
}
每次运行时,我都会从端点返回以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<soapenv:Fault><faultcode>soapenv:VersionMismatch</faultcode><faultstring>Only SOAP 1.1 or SOAP 1.2 messages are supported in the system</faultstring><detail></detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
有人对如何解决这个问题有任何建议吗?老实说,我在 SOAP 方面的经验是有限的,而且我在这个问题上花了两天多的时间。在 Postman 中执行相同的 XML 正文成功发布,没有问题。
【问题讨论】:
-
你似乎有正确的正文,但标题呢?
-
在 Postman 中运行相同的字符串会返回正确的数据;我将在我的代码中玩耍,看看我是否能注意到任何潜在的差异。
-
Postman 将向请求中添加其他标头(不是 Soap 标头、HTTP 标头)。
标签: c# asp.net-core soap