繁体   English   中英

C#Soap 1.2 Web服务

[英]C# Soap 1.2 WebService

我在调用外部第三方肥皂网络服务时遇到问题:(正在从客户端返回)错误是“远程服务器返回错误:(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>

生的

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

谢谢@扫帚! 我已经使用提琴手进行了一些测试,并且得到了以下消息:

          <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中定义动作

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。 只需要注意双引号!!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM