C# Console App - Can't retrieve SOAP 1.2 response from Web Service

  • Thread starter Thread starter Treading_Water
  • Start date Start date
T

Treading_Water

Guest
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 = "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);
}
}
}
}

Continue reading...
 
Back
Top