How to create a soap client without WSDL

jojo picture jojo · Jan 27, 2010 · Viewed 7.6k times · Source

i need to visit a secure web service, every request in the header need to carry a token.

i know the endpoint to the web service, i also know how to create the token.

but i cannot see the WSDL for the webservice.

is there a way in C#, to create a soap client, without the WSDL file.

Answer

Brent Matzelle picture Brent Matzelle · Nov 5, 2010

I have verified that this code, which uses the HttpWebRequest class, works:

// Takes an input of the SOAP service URL (url) and the XML to be sent to the
// service (xml).  
public void PostXml(string url, string xml) 
{
    byte[] bytes = Encoding.UTF8.GetBytes(xml);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentLength = bytes.Length;
    request.ContentType = "text/xml";

    using (Stream requestStream = request.GetRequestStream())
    {
       requestStream.Write(bytes, 0, bytes.Length);
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("POST failed with HTTP {0}", 
                                           response.StatusCode);
            throw new ApplicationException(message);
        }
    }
}

You will need to create the proper SOAP envelope and pass that in as the "xml" variable. It takes some reading. I found this SOAP Tutorial to be helpful.