我正在尝试将XMLDocument
发布到网址。这就是我到目前为止所做的:
var uri = System.Configuration.ConfigurationManager.AppSettings["Url"];
var template = System.Configuration.ConfigurationManager.AppSettings["Template"];
XmlDocument reqTemplateXml = new XmlDocument();
reqTemplateXml.Load(template);
reqTemplateXml.SelectSingleNode("appInfo/appNumber").InnerText = x;
reqTemplateXml.SelectSingleNode("appInfo/coappNumber").InnerText = y;
WebRequest req = null;
WebResponse rsp = null;
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
rsp = req.GetResponse();
我想弄清楚的是如何将XmlDocument
加载到WebRequest
对象,以便将其发布到该网址。
答案 0 :(得分:14)
你需要在调用req.GetResponse()
之前写入RequestStream。
using (var writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(xml);
}