C# · 12月 30, 2021

c# – 将XML发布到URL

我试图将一个 XMLDocument发布到一个URL.这是我到目前为止 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对象,以便将其发布到该URL.

解决方法 您需要在调用req.GetResponse()之前写入RequestStream. using (var writer = new StreamWriter(req.GetRequestStream())) { writer.Write(xml); }