Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending and receiving SOAP messages

I am writing a web service client in C# and do not want to create and serialize/deserialize objects, but rather send and receive raw XML.

Is this possible in C#?

like image 473
jean Avatar asked Dec 07 '09 21:12

jean


1 Answers

Here is part of an implementation I just got running based on John M Gant's example. It is important to set the content type request header. Plus my request needed credentials.

protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
{
    var wr = WebRequest.Create(soapMessage.Uri);
    wr.ContentType = "text/xml;charset=utf-8";
    wr.ContentLength = soapMessage.ContentXml.Length;

    wr.Headers.Add("SOAPAction", soapMessage.SoapAction);
    wr.Credentials = soapMessage.Credentials;
    wr.Method = "POST";
    wr.GetRequestStream().Write(Encoding.UTF8.GetBytes(soapMessage.ContentXml), 0, soapMessage.ContentXml.Length);

    return wr;
}

public interface ISoapMessage
{
    string Uri { get; }
    string ContentXml { get; }
    string SoapAction { get; }
    ICredentials Credentials { get; }
}
like image 144
CRice Avatar answered Oct 29 '22 00:10

CRice