Sending and receiving SOAP messages

15,106

Solution 1

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; }
}

Solution 2

You can use the System.Net classes, such as HttpWebRequest and HttpWebResponse to read and write directly to an HTTP connection.

Here's a basic (off-the-cuff, not compiled, non-error-checking, grossly oversimplified) example. May not be 100% correct, but at least will give you an idea of how it works:

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(url);
req.ContentLength = content.Length;
req.Method = "POST";
req.GetRequestStream().Write(Encoding.ASCII.GetBytes(content), 0, content.Length);
HttpWebResponse resp = (HttpWebResponse) req.getResponse();
//Read resp.GetResponseStream() and do something with it...

This approach works well. But chances are whatever you need to do can be accomplished by inheriting the existing proxy classes and overriding the members you need to have behave differently. This type of thing is best reserved for when you have no other choice, which is not very often in my experience.

Solution 3

Yes - you can simply declare the inputs and outputs as XmlNode's

[WebMethod]
public XmlNode MyMethod(XmlNode input);

Solution 4

You can have your web service method return a string containing the xml, but do heed the comment above about making things more error-prone.

Share:
15,106
Admin
Author by

Admin

Updated on July 14, 2022

Comments

  • Admin
    Admin almost 2 years

    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#?

  • CRice
    CRice over 13 years
    +1 thanks for the post. I posted my implementation of your suggestion also.
  • Data-Base
    Data-Base over 13 years
    Thanks allot, but how to use it?
  • CRice
    CRice over 13 years
    WebRequest request = CreateRequest(soapMessage);
  • CRice
    CRice over 13 years
    WebResponse webResponse = request.GetResponse();
  • hellowahab
    hellowahab about 9 years
    @CRice Do I have to create a new class of SoapMessage while implementing ISoapMessage Interface?
  • CRice
    CRice about 9 years
    @hellowahab Just put the interface on any class and pass that in. Or remove the interface as a parameter of CreateRequest and use each of the four parameters found in ISoapMessage instead.