Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 
like image 614
MaLKaV_eS Avatar asked Aug 14 '09 10:08

MaLKaV_eS


People also ask

Is WSDL mandatory for SOAP?

The WSDL Generator component is not essential for using SOAP. Administrators can still write service calls to Content Server in SOAP if needed. The WSDL Generator provides flexibility in altering existing client applications.

How do you call a service reference in C#?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.


2 Answers

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.

like image 179
MaLKaV_eS Avatar answered Sep 29 '22 10:09

MaLKaV_eS


In my opinion, there is no excuse for a SOAP web service to not supply a WSDL. It need not be dynamically generated by the service; it need not be available over the Internet. But there must be a WSDL, even if they have to send it to you on a floppy disk thumb drive!

If you have any ability to complain to the providers of this service, then I urge you to do so. If you have the ability to push back, then do so. Ideally, switch service providers, and tell these people it's because they didn't provide a WSDL. At the very least, find out why they don't think it's important.

like image 34
John Saunders Avatar answered Sep 29 '22 10:09

John Saunders