我正在尝试编写一个函数,该函数可以在给定方法的名称和 Web 服务的 URL 的情况下从 Web 服务调用 Web 方法。我在博客上找到了一些代码,除了一个细节外,它做得很好。它还要求提供请求 XML。这里的目标是从 web 服务本身获取请求 XML 模板。我确信这是可能的,因为如果我在浏览器中访问 Web 服务的 URL,我可以同时看到请求和响应 XML 模板。
这是以编程方式调用 webmethod 的代码:
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());