Invoking an ASP.NET web service method via an http request

13,870

I don't know why I am doing this but here's an example of invoking a web service manually. Please promise to never use this in a production code.

Suppose you had the following SOAP service:

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld(Foo foo)
    {
        return "Hello World";
    }
}

You can invoke it manually like this:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("SOAPAction", "\"http://tempuri.org/HelloWorld\"");
            client.Headers.Add("Content-Type", "text/xml; charset=utf-8");
            var payload = @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><HelloWorld xmlns=""http://tempuri.org/""><foo><Id>1</Id><Name>Bar</Name></foo></HelloWorld></soap:Body></soap:Envelope>";
            var data = Encoding.UTF8.GetBytes(payload);
            var result = client.UploadData("http://localhost:1475/Service1.asmx", data);
            Console.WriteLine(Encoding.Default.GetString(result));
        }
    }
}
Share:
13,870
Tola Odejayi
Author by

Tola Odejayi

Updated on June 05, 2022

Comments

  • Tola Odejayi
    Tola Odejayi about 2 years

    I want to invoke an ASP.NET web service via an http POST request using C# (i.e. I don't want to use the SoapHttpClientProtocol object generated by running wsdl.exe).

    As far as I can tell, the process involves:

    1. creating an HttpWebRequest object which points to the url/method of the web service, with the method;

    2. Creating a SOAP xml envelope;

    3. Serialising any parameters I want to pass to the web method using an XmlSerializer;

    4. Making the request, and parsing the response.

    I would like to do this without having to copy and use generated code.

    (1) seems pretty straightforward;

    (2) I don't know if the envelope here is standard, or how it should change depending on the webservice method I am calling. I guess I might need to add custom soap headers if required by the service?

    (3) What is the process of doing this? I assume that I need to do something like this:

    MyClass myObj;
    XmlSerializer ser = new XmlSerializer(myObj.GetType());
    TextWriter writer = new StringWriter();
    ser.Serialize(writer, myObj);
    string soapXml = writer.ToString();
    writer.Close();
    

    Also, I guess I should add the soapXml to the soap:Body element

    (4) I believe I should extract and deserialize the contents of the soap:Body element as well. Is it OK to use the reverse of the process in (3)?

    Thanks,

    K.

  • Tola Odejayi
    Tola Odejayi over 14 years
    Darin, thanks. Just to clarify, is there something inherently bad in invoking webservice methods at the http request level? Also, given a web method signature and arguments, is there a generic way of constructing the soap body?
  • Tola Odejayi
    Tola Odejayi over 14 years
    I'd like to nominate this as the answer, as it's been partly helpful, but can anyone help to clarify the question I asked above about creating a soap body given a webmethod's argument?
  • Darin Dimitrov
    Darin Dimitrov over 14 years
    @Shoko, the SOAP protocol is a pretty complicated protocol and for this reason the framework gives you the tools to work generate clients from WSDL: svcutil.exe (msdn.microsoft.com/en-us/library/aa347733.aspx) and the older wsdl.exe. They generate strongly typed classes and allow you to invoke web service methods in an easy way, without writing a single line of XML.