无法从C#控制台应用程序中的SOAP 1.2 Web请求获得响应

时间:2018-08-16 19:04:15

标签: c# .net soap

我无法使用C#代码访问SOAP 1.2 Web服务。在此示例中,我遵循的代码为Web Service SOAP Call

它对我不起作用。我的Web服务URL可在SOAPUI中使用,但是我的C#代码无法得到响应。我收到“ 500内部错误”。状态显示为“ ProtocolError”。 ProtocolError

我正在通过Visual Studio 2017编辑器运行它。

using System;
using System.IO;
using System.Net;
using System.Xml;

namespace testBillMatrixConsole
{
    class Program
    {
        static void Main(string[] args)
        {
                string _url = @"https://service1.com/MSAPaymentService/MSAPaymentService.asmx";
                var _action = @"http://schemas.service1.com/v20060103/msapaymentservice/AuthorizePayment";
            try
            {
                XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
                HttpWebRequest webRequest = CreateWebRequest(_url, _action);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }
                    Console.Write(soapResult);
                }



            }
            catch (WebException ex)
                {

                throw;//ex.Message.ToString();

            }

        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(@"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:msap=""http://schemas.service1.com/v20060103/msapaymentservice""><soap:Body><msap:AuthorizePayment><!--Optional:--><msap:clientPaymentId>?</msap:clientPaymentId><msap:amount>?</msap:amount><msap:method>?</msap:method><!--Optional:--><msap:parameters><!--Zero or more repetitions:--><msap:PaymentParameter><!--Optional:--><msap:Name>?</msap:Name><!--Optional:--><msap:Value>?</msap:Value></msap:PaymentParameter></msap:parameters></msap:AuthorizePayment></soap:Body></soap:Envelope>");
            return soapEnvelopeDocument;
        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

0 个答案:

没有答案