5

I'm trying to write a function that can call a webmethod from a webserive given the method's name and URL of the webservice. I've found some code on a blog that does this just fine except for one detail. It requires that the request XML be provided as well. The goal here is to get the request XML template from the webservice itself. I'm sure this is possible somehow because I can see both the request and response XML templates if I access a webservice's URL in my browser.

This is the code which calls the webmethod programmatically:

XmlDocument doc = new XmlDocument();
//this is the problem. I need to get this automatically
doc.Load("../../request.xml"); 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/dummyws/dummyws.asmx?op=HelloWorld");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
Stream stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
StreamReader r = new StreamReader(stm);
Console.WriteLine(r.ReadToEnd());
Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
hancock
  • 53
  • 1
  • 1
  • 3
  • how can you call a method (web service or otherwise) if you don't know up front what parameters it requires? – Mitch Wheat Jun 04 '10 at 11:08
  • Let's assume for now that these webmethods don't require any parameters. – hancock Jun 04 '10 at 11:17
  • Does the web service expose a WSDL description? – dariom Jun 04 '10 at 11:33
  • Yes, the webservice exposes a WSDL description. – hancock Jun 04 '10 at 11:40
  • 1
    So can't you use the WSDL file as the definition for your request XML? Or, easier, generate a proxy for the web service from the WSDL? – dariom Jun 04 '10 at 11:56
  • I guess I could manually parse the WSDL and infer the SOAP body but I thought maybe there is a more elegant way to this. Can I generate the WS proxy at runtime from the WSDL? – hancock Jun 04 '10 at 12:04

2 Answers2

2

Following on from the comments above. If you have a WSDL file that describes your service you use this as the information required to communicate with your web service.

Using a proxy class to communicate with your service proxy is an easy way to abstract yourself from the underlying plumbing of HTTP and XML.

There are ways of doing this at run-time - essentially generating the code that Visual Studio generates when you add a web service reference to your project.

I've used a solution that was based on: this newsgroup question, but there are also other examples out there.

dariom
  • 4,413
  • 28
  • 42
0

FYI, your code is missing using blocks. It should be more like this:

XmlDocument doc = new XmlDocument();
//this is the problem. I need to get this automatically
doc.Load("../../request.xml"); 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/dummyws/dummyws.asmx?op=HelloWorld");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";

using (Stream reqstm = req.GetRequestStream())
{
    doc.Save(reqstm);
}

using (WebResponse resp = req.GetResponse())
{
    using (Stream respstm = resp.GetResponseStream())
    {
        using (StreamReader r = new StreamReader(respstm))
        {
            Console.WriteLine(r.ReadToEnd());
        }    
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397