以编程方式将 xml 数据发布到资源并序列化结果

发布于 2024-11-19 16:45:55 字数 632 浏览 1 评论 0原文

我正在尝试使用来自第三方提供商的相对较新的 API。基本用法是 POST 查询,格式为 xml。因此,理论上,POST uri 看起来像:

http://provideraddress.com/?xml=< em>myxml

反过来,我应该收到 xml 格式的响应。

已经有一段时间了,所以我正在努力寻找最好的方法来做到这一点(事实上,我不太关心“最好”,并且会满足于让事情正常工作)。

到目前为止,我已经尝试使用 WebClient 和 WebRequest。在手动将 xml 数据构建为字符串之后,最新的尝试使用了后者。我尝试将“xml”参数附加到 uri,并将其写入 StreamWriter。首先,我得到某种 WebResponse 对象,但内容长度始终为 -1,并且内容类型报告为 text/html,这两者似乎都不合适。其次,我不确定如何解析 WebResponse 或将其内容序列化到我的对象图中。

我用谷歌搜索了很多,这些例子不稳定或过时。我不想处理文件系统写入。理想情况下,我能够填充自己的请求对象图,将其序列化为 xml,发送 POST,并接收可以序列化回响应对象图的内容。

任何帮助将不胜感激。

I am trying to work with a relatively new API from a 3rd party provider. The basic usage is to POST a query, formatted as xml. So, theoretically, the POST uri would look like:

http://provideraddress.com/?xml=myxml

In turn, I should receive an xml formatted response.

It's been awhile, so I'm struggling with the best way to do this (in fact, I could care less about "best", and would settle for just getting the thing to work).

So far I've attempted to use WebClient as well as WebRequest. The latest attempt uses the latter, after building the xml data manually as a string. I've tried appending the "xml" param to the uri, as well as writing it to a StreamWriter. First, I get some sort of WebResponse object back, but the content length is always -1, and the content type is reported as text/html, neither of which seems appropriate. Second, I'm unsure of how I would parse the WebResponse or serialize it's content into my object graph.

I've googled quite a bit and the examples are erratic or outdated. I don't want to deal with file system writing. Ideally, I'd be able to fill my own request object graph, serialize that into xml, send the POST, and receive something that can be serialized back into my respond object graph.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-11-26 16:45:55

最简单的方法是使用 WebClient:

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "text/xml";
            string xml = @"<foo><bar>baz</bar></foo>";
            string url = "http://provideraddress.com/?xml=myxml";
            string response = client.UploadString(url, xml);
            Console.WriteLine(response);
        }
    }
}

现在就生成请求字符串而言,您可以使用 .NET 中可用的多种方法之一:XDocumentXmlWriterXmlDocument (这个开始变老了,您可能应该更喜欢 XDocument)或使用 XmlSerializer, ... 响应 XML = 相同>您可以使用 XDocument、XmlReader、XmlDocument 解析它,或使用 XmlSerializer 将其反序列化回对象图。

现在,如果服务器以内容类型 text/html 进行响应,但没有返回任何响应,这可能是因为您发布的 XML 不是服务器期望的格式,您没有提供一些 HTTP 请求标头服务器预期,服务器在处理您的请求时遇到一些错误,...阅读第 3 方 API 的文档并确保您遵守其格式。有些 API 可能需要首先进行身份验证。因此,您需要使用用户名和密码向某个 url 发送请求,如果身份验证成功,服务器会向您发送回您应该在所有后续请求中发送的令牌或 cookie。

The easiest would be to use a WebClient:

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "text/xml";
            string xml = @"<foo><bar>baz</bar></foo>";
            string url = "http://provideraddress.com/?xml=myxml";
            string response = client.UploadString(url, xml);
            Console.WriteLine(response);
        }
    }
}

Now as far as generating the request string is concerned you could use one of the many methods available in .NET: XDocument, XmlWriter, XmlDocument (this one starts getting old, you should probably prefer a XDocument instead) or serialize an object graph into string using XmlSerializer, ... Same for the response XML => you could parse it using XDocument, XmlReader, XmlDocument or deserialize it back to an object graph using XmlSerializer.

Now if the server responds with content type text/html and returns you no response that is probably because the XML you POSTed was not in the format that the server expected, you didn't supply some HTTP request header that the server expected, the server encountered some error processing your request, ... Read the documentation of the 3rd party API and ensure that you are respecting its format. There are some APIs that might require an authentication first. So that you would need to send a request to some url with a username and password and if the authentication succeeds the server sends you back a token or a cookie that you should send on all subsequent requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文