Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a soap client without WSDL

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.

like image 924
jojo Avatar asked Jan 27 '10 22:01

jojo


2 Answers

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.

like image 137
Brent Matzelle Avatar answered Oct 19 '22 22:10

Brent Matzelle


A SOAP client is simply an HTTP client with more stuff in it. See the HttpWebRequest class. You'll then have to create your own SOAP message, perhaps using XML Serialization.

like image 29
John Saunders Avatar answered Oct 20 '22 00:10

John Saunders