C# – How to call a web service with no wsdl in .net

cnetsoapweb serviceswsdl

I have to connect to a third party web service that provides no wsdl nor asmx. The url of the service is just http://server/service.soap

I have read this article about raw services calls, but I'm not sure if this is what I'm looking for.

Also, I've asked for wsdl files, but being told that there are none (and there won't be).

I'm using C# with .net 2.0, and can't upgrade to 3.5 (so no WCF yet). I think that third party is using java, as that's the example they have supplied.

Thanks in advance!

UPDATE Get this response when browsing the url:

<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>
Cannot find a Body tag in the enveloppe
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Best Answer

Well, I finally got this to work, so I'll write here the code I'm using. (Remember, .Net 2.0, and no wsdl to get from web service).

First, we create an HttpWebRequest:

public static HttpWebRequest CreateWebRequest(string url)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAP:Action");
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

Next, we make a call to the webservice, passing along all values needed. As I'm reading the soap envelope from a xml document, I'll handle the data as a StringDictionary. Should be a better way to do this, but I'll think about this later:

public static XmlDocument ServiceCall(string url, int service, StringDictionary data)
{
    HttpWebRequest request = CreateWebRequest(url);

    XmlDocument soapEnvelopeXml = GetSoapXml(service, data);

    using (Stream stream = request.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }

    IAsyncResult asyncResult = request.BeginGetResponse(null, null);

    asyncResult.AsyncWaitHandle.WaitOne();

    string soapResult;
    using (WebResponse webResponse = request.EndGetResponse(asyncResult))
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }

    File.WriteAllText(HttpContext.Current.Server.MapPath("/servicios/" + DateTime.Now.Ticks.ToString() + "assor_r" + service.ToString() + ".xml"), soapResult);

    XmlDocument resp = new XmlDocument();

    resp.LoadXml(soapResult);

    return resp;
}

So, that's all. If anybody thinks that GetSoapXml must be added to the answer, I'll write it down.