c# – 将XML发布到URL

前端之家收集整理的这篇文章主要介绍了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);
    }
原文链接:https://www.f2er.com/csharp/94380.html

猜你在找的C#相关文章