C#Console App - 无法从Web Service检索SOAP 1.2响应 [英] C# Console App - Can't retrieve SOAP 1.2 response from Web Service

查看:80
本文介绍了C#Console App - 无法从Web Service检索SOAP 1.2响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我无法在控制台应用程序中使用C#代码访问SOAP 1.2 Web服务。它对我不起作用。我的Web服务URL在SOAPUI中工作,但我无法在C#代码中获得响应。我收到"500内部错误"。状态显示为"ProtocolError"。
我是从Visual Studio 2017编辑器运行的。我做错了什么?

I am unable to hit a SOAP 1.2 web service with C# code, inside a console app. It does not work for me. My web service URL works in SOAPUI but I can't get a response in my C# code. I get a "500 Internal error." The status reads "ProtocolError." I am running this from my Visual Studio 2017 editor. What am I doing wrong?

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 = "application/soap+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);
            }
        }
    }
}






推荐答案

您写的这个Web服务?如果是这样,那么HTTP 500错误意味着抛出了未处理的.Net异常。您在Web服务中没有任何异常处理,因此Web服务器吞下了异常并通过HTTP 500错误。

This Web service you wrote? If so, then the HTTP 500 error means that an unhandled .Net exception was thrown. You don't have any exception handling in the Web service so the Web server swallowed the exception and through the HTTP 500 error.

协议错误可能只是表面就像HTTP 500一样在这种情况下很肤浅。 

The protocol error could just be superficial just like the HTTP 500 is superficial in this situation. 

您需要在服务中实现一些全局异常处理逻辑并记录异常,以便您可以看到它。

You need to implement some global exception handling logic in the service and log the exception so you can see it.

您可以使用的另一个工具是PostMon,它可能允许您查看导致HTTP 500的异常。 

Another tool you can use is PostMon that may allow you to see the exception that caused the HTTP 500. 

可以通过以下链接处理Web服务。

Web services can be addressed at the below link.

  https://forums.asp.net/28.aspx/1?WCF + ASMX +和+其他+网络+服务

 https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services


这篇关于C#Console App - 无法从Web Service检索SOAP 1.2响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆