C#Soap 1.2 WebService

时间:2018-03-12 16:31:39

标签: c# soap httpwebrequest content-type

调用外部第三方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(我已将域名更改为&#39; test&#39;)

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.........

SoapUI

1 个答案:

答案 0 :(得分:0)

谢谢@Broom! 我用fiddler做了一些测试,我得到了以下信息:

          <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中的Action

Content-Type: application/soap+xml;charset=UTF-8;action="http://www.test.com/test/Services/Outage/IOutageService/GetLocations"

然后我在WebRequest中更改了Content-Type

var contentString =  @"application/soap+xml;charset=UTF-8;action=""http://www.test/test/Services/Outage/IOutageService/GetLocations""";
        webRequest.ContentType = contentString;

代码从Web服务接收xml。 只需要注意双引号!!!!