我在这里尝试做的是使用 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/