我正在使用 Litle 的支付系统,我必须创建一个 XML 文档,根据他们的 XSD 对其进行验证,然后通过 HTTP post 进行传输。
有什么建议吗?
目前我正计划使用 XmlDocument 类来创建和验证 XML,并且仍在研究使用什么将其传输到服务器并取回响应(也是 XML 格式)。
最好的方法是什么?
没有:没有基于网络服务的 API。只是手动格式化 XML 和帖子。尽管如果有一些库可以自动执行此操作,那将会非常有帮助。
最佳答案
你可以使用 WebClient发送 HTTP 请求。
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "text/xml";
// you could add any other HTTP request headers that the server expects
// fetch the XML that you want to POST
string xml = ...
byte[] data = Encoding.UTF8.GetBytes(xml);
// POST the XML to the given address
byte[] result = client.UploadData("http://example.com", data);
// TODO: process the result, for example load it in a XDocument
using (var stream = new MemoryStream(result))
using (var reader = XmlReader.Create(stream))
{
var doc = XDocument.Load(reader);
}
}
关于.net - 创建 XML 并通过 HTTP post 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586539/