gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:47:09 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com