gpt4 book ai didi

带有 XML 结构化数据的 C# HttpWebRequest

转载 作者:可可西里 更新时间:2023-11-01 08:16:33 24 4
gpt4 key购买 nike

我正在开发第三方网络服务的客户端。目的是我将 xml 文件发送到服务器。我应该如何将 xml 文件附加到 httpwebrequest?需要什么内容类型?更多建议?

我不能使用 mtom 或 dime.ie,因为我正在使用 httpwebrequest。我也无法使用 WCF。

最佳答案

这是使用 HttpWebRequest 发送 XML 结构化数据的非常基本的方法(顺便说一句,您需要使用 request.ContentType = "application/xml";):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(myUrl));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";

XElement redmineRequestXML =
new XElement("issue",
new XElement("project_id", 17)
);

byte[] bytes = Encoding.UTF8.GetBytes(redmineRequestXML.ToString());

request.ContentLength = bytes.Length;

using (Stream putStream = request.GetRequestStream())
{
putStream.Write(bytes, 0, bytes.Length);
}

// Log the response from Redmine RESTful service
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Logger.Info("Response from Redmine Issue Tracker: " + reader.ReadToEnd());
}

我在我的一个项目 ( NBug ) 中使用它向我的 Redmine 问题跟踪器提交问题报告,该跟踪器通过 Web 请求(通过 POST)接受 XML 结构化数据。如果您需要更多示例,可以在此处获得几个功能齐全的示例:http://nbug.codeplex.com/SourceControl/list/changesets (单击右侧“最新版本”标签下的“浏览”,然后导航至“NBug\Submit\Tracker\Redmine.cs”)

关于带有 XML 结构化数据的 C# HttpWebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5653743/

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