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());
6
  • how can you call a method (web service or otherwise) if you don't know up front what parameters it requires? Commented Jun 4, 2010 at 11:08
  • Let's assume for now that these webmethods don't require any parameters. Commented Jun 4, 2010 at 11:17
  • Does the web service expose a WSDL description? Commented Jun 4, 2010 at 11:33
  • Yes, the webservice exposes a WSDL description. Commented Jun 4, 2010 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? Commented Jun 4, 2010 at 11:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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());
        }    
    }
}

1 Comment

You're right, but I used dariom's solution which does not employ that piece of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.