我在调用外部第三方 SOAP Web 服务时遇到问题:我得到的错误是"远程服务器返回错误:(500( 内部服务器错误"。(来自客户端(,但问题是当我使用 SoapUI 5.4.0(在同一用户下的同一台 PC 上(调用时,它运行良好。该代码适用于任何其他 Web 服务。
有人可以给我任何想法吗?
using System;
using System.IO;
using System.Net;
using System.Xml;
namespace ConsoleApp1
{
class Program
/// Soap WebService call
public static void Execute()
{
HttpWebRequest request = CreateWebRequest();
request.Credentials = CredentialCache.DefaultCredentials;
XmlDocument soapEnvelopeXml = new XmlDocument();
NewMethod(soapEnvelopeXml);
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
Console.WriteLine("Done!");
Console.ReadKey();
}
}
}
private static void NewMethod(XmlDocument soapEnvelopeXml)
{
soapEnvelopeXml.LoadXml(xml: @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:out=""http://www.test.com/test/Services/Outage""><soap:Header/><soap:Body><out:GetLocations/></soap:Body></soap:Envelope>");
}
/// Create a soap webrequest to [Url]
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://test:557/OutageService.svc");
webRequest.ContentType = @"application/soap+xml;charset=UTF-8";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
static void Main(string[] args)
{
Execute();
}
}
}
和 SoapUI 5.4.0(我已将域名更改为"测试"(
.XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:out="http://www.test.com/test/Services/Outage">
<soap:Header/>
<soap:Body>
<out:GetLocations/>
</soap:Body>
</soap:Envelope>
.RAW
POST http://test:557/OutageService.svc HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="http://www.test.com/test/Services/Outage/IOutageService/GetLocations"
Content-Length: 390
Host: test:557
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
响应
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 155883
Content-Type: application/soap+xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 12 Mar 2018 16:09:46 GMT
<s:Envelope xmlns:s="http://www.w3.org/2003/05/so.........
肥皂用户界面
谢谢你@Broom!我已经使用提琴手做了一些测试,我得到以下消息:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/fault</a:Action></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Sender
</s:Value><s:Subcode><s:Value>
a:ActionMismatch</s:Value></s:Subcode></s:Code><s:Reason><s:Text xml:lang="en-US">
The SOAP action specified on the message, '"http://www.test.com/test/Services/Outage/IOutageService/GetLocations"',
does not match the HTTP SOAP Action, 'http://www.test.com/test/Services/Outage/IOutageService/GetLocations'.
</s:Text></s:Reason><s:Detail><a:ProblemHeaderQName>a:Action</a:ProblemHeaderQName></s:Detail></s:Fault></s:Body></s:Envelope>
所以在我的代码中我改变了
private static void NewMethod(XmlDocument soapEnvelopeXml)
{
soapEnvelopeXml.LoadXml(xml: @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:out=""http://www.test.com/test/Services/Outage""><soap:Header/><soap:Body><out:GetLocations/></soap:Body></soap:Envelope>");
}
到以下
soapEnvelopeXml.LoadXml(xml: @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:out=""http://www.test.com/test/Services/Outage""><soap:Header xmlns:wsa=""http://www.w3.org/2005/08/addressing""><wsa:Action>http://www.test.com/test/Services/Outage/IOutageService/GetLocations</wsa:Action></soap:Header><soap:Body><out:GetLocations/></soap:Body></soap:Envelope>");
在内容类型中定义操作
Content-Type: application/soap+xml;charset=UTF-8;action="http://www.test.com/test/Services/Outage/IOutageService/GetLocations"
然后我在WebRequest中更改了
内容类型var contentString = @"application/soap+xml;charset=UTF-8;action=""http://www.test/test/Services/Outage/IOutageService/GetLocations""";
webRequest.ContentType = contentString;
代码从 Web 服务接收 xml。只需要注意双引号!!!