c# - 我如何使用 post 方法调用 Rest Web 服务并在 C# 中发送 xml 数据

标签 c# xml rest post

我在这里尝试做的是使用 xml 数据向 Rest web 服务发出 post 请求。

这是我现在拥有的,但我不确定如何传递我的 xml 数据

            XElement xml = new XElement("MatchedOptions",
               from m in _matchedOptionsList
               select new XElement("Listing",
                       new XElement("DomainID", _trafficCopInputs.DomainID),
                       new XElement("AdSource", _trafficCopInputs.AdSource),
                       new XElement("Campaign", _trafficCopInputs.Campaign),
                       new XElement("AdGroup", _trafficCopInputs.AdGroup),
                       new XElement("RedirectURL", m.RedirectPath),
                       new XElement("FunnelKeyword", m.FunnelKeyword)));

            HttpWebRequest req = WebRequest.Create("http://something.com/")
                 as HttpWebRequest;


            req.Method = "POST";
            req.ContentType = "text/xml";
            req.ContentLength = 0;
            StreamWriter writer = new StreamWriter(req.GetRequestStream());
            writer.WriteLine(xml.ToString());

最佳答案

我使用 WebClient 类:

WebClient webClient = new WebClient();
using (webClient)
{
   requestInterceptor.OnRequest(webClient);
   var enc = new ASCIIEncoding();
   return enc.GetString(webClient.UploadData(uri, enc.GetBytes(dataAsString)));
}

关于c# - 我如何使用 post 方法调用 Rest Web 服务并在 C# 中发送 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1460197/

相关文章:

c# - MVVM 的正确方法

C# 进阶 String.Split

Android:始终显示按钮

java - 初始应用程序介绍启动后启动启动屏幕

REST 工具支持开发和测试

java - Jersey HTTP 响应 200 OK 但返回内容错误(用户名/密码不正确)

c# - http rest api - 如何将响应字符串字段加载到变量

c# - 文件夹权限 : Full control granted to all users

C# : catch all errors/exceptions of a mixed managed/unmanaged process

java - Intellij 完成贡献者