将SOAPSonar转换为C#时发生XML验证错误

时间:2018-06-20 14:40:56

标签: c# .net xml soap wsdl

我可以在SOAPSonar中运行以下SOAP请求(通过WSDL生成),而不会出现问题,并且可以获取数据:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ng1="http://www.sx3.com/GET_PERSON_DETAILS">
  <soap:Body>
    <ng1:ParDetailsRequest>
      <ng1:ParRefno>1200</ng1:ParRefno>
      <ng1:FormerNameChkInd>Y</ng1:FormerNameChkInd>
      <ng1:RacPay>REVENUE ACCOUNT</ng1:RacPay>
      <ng1:CurrentParInd>Y</ng1:CurrentParInd>
      <ng1:CurrentAppInd>A</ng1:CurrentAppInd>
      <ng1:CurrentTcyInd>A</ng1:CurrentTcyInd>
      <ng1:RtnContactAddrInd>Y</ng1:RtnContactAddrInd>
      <ng1:RtnContactDetailsInd>N</ng1:RtnContactDetailsInd>
      <ng1:RtnAusInd>Y</ng1:RtnAusInd>
      <ng1:RtnAppInd>Y</ng1:RtnAppInd>
      <ng1:RtnTcyInd>Y</ng1:RtnTcyInd>
      <ng1:RtnOtherFieldsInd>N</ng1:RtnOtherFieldsInd>
      <ng1:RtnFmrNameHistInd>Y</ng1:RtnFmrNameHistInd>
      <ng1:RtnNotepadsInd>N</ng1:RtnNotepadsInd>
    </ng1:ParDetailsRequest>
  </soap:Body>
</soap:Envelope>

我尝试按以下方式(基于this answer)在C#中实现请求:

public static String CallWebService(String URL, int RefNum)
{

    XmlDocument Envelope = CreateEnvelope(RefNum);
    HttpWebRequest Request = CreateRequest(URL);
    InsertEnvelope(Request, Envelope);

    IAsyncResult AsyncResult = Request.BeginGetResponse(null, null);
    AsyncResult.AsyncWaitHandle.WaitOne();

    String Result;

    using (WebResponse Response = Request.EndGetResponse(AsyncResult))
    {
        using (StreamReader Reader = new StreamReader(Response.GetResponseStream())) Result = Reader.ReadToEnd();
    }

    return Result;

}

private static XmlDocument CreateEnvelope(int RefNum)
{

    XmlDocument Envelope = new XmlDocument();

    Envelope.LoadXml(
        String.Format(@"
            <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:ng1=""http://www.sx3.com/GET_PERSON_DETAILS"">
                <soap:Body>
                    <ng1:ParDetailsRequest>
                        <ng1:ParRefno>{0}</ng1:ParRefno>
                        <ng1:FormerNameChkInd>Y</ng1:FormerNameChkInd>
                        <ng1:RacPay>REVENUE ACCOUNT</ng1:RacPay>
                        <ng1:CurrentParInd>Y</ng1:CurrentParInd>
                        <ng1:CurrentAppInd>A</ng1:CurrentAppInd>
                        <ng1:CurrentTcyInd>A</ng1:CurrentTcyInd>
                        <ng1:RtnContactAddrInd>Y</ng1:RtnContactAddrInd>
                        <ng1:RtnContactDetailsInd>Y</ng1:RtnContactDetailsInd>
                        <ng1:RtnAusInd>Y</ng1:RtnAusInd>
                        <ng1:RtnAppInd>Y</ng1:RtnAppInd>
                        <ng1:RtnTcyInd>Y</ng1:RtnTcyInd>
                        <ng1:RtnOtherFieldsInd>N</ng1:RtnOtherFieldsInd>
                        <ng1:RtnFmrNameHistInd>Y</ng1:RtnFmrNameHistInd>
                        <ng1:RtnNotepadsInd>N</ng1:RtnNotepadsInd>
                    </ng1:ParDetailsRequest>
                </soap:Body>
            </soap:Envelope>
        ", RefNum.ToString())
    );

    return Envelope;

}

private static HttpWebRequest CreateRequest(String URL)
{

    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
    //Request.Headers.Add(@"SOAP:Action");
    Request.ContentType = "text/xml;charset=\"utf-8\"";
    Request.Accept = "text/xml";
    Request.Method = "POST";
    return Request;

}

private static void InsertEnvelope(HttpWebRequest Request, XmlDocument Envelope)
{
    using (Stream Stream = Request.GetRequestStream()) Envelope.Save(Stream);
}

但是我收到以下错误消息:

  • ORA-31043:架构中未全局定义元素“信封” 'GET_PERSON_DETAILS.xsd'
  • 提供的XML验证失败。联系支持。

1 个答案:

答案 0 :(得分:0)

这里的问题在于SOAPAction标头。 我从此更改了:

Request.Headers.Add(@"SOAP:Action");

对此:

Request.Headers.Add("SOAPAction", (URL + "gateway"));