3

FluUrl在处理 Json/UrlEncoded 请求方面做得很好。但是,文档并没有真正指出如何处理其他请求类型,例如text/xml.

使用 FluUrl 进行 XML POST 的最佳方式是什么?

这(访问底层HttpClient)有点违背了使用 FluUrl 的目的,因为您需要再次构建 URI 和内容:

var result = await "http://someUrl"
                   .AppendPathSegment(pathSegment)
                   .SetQueryParam("name", name)
                   .WithBasicAuth(_userName, _apiToken)
                   .HttpClient
                   .PostAsync(<uri>, <content>);
4

5 回答 5

4

XML 支持尚未加入,但已记录,这是我希望为 Flurl.Http 1.0 提供的为数不多的剩余功能之一。(令人惊讶的是,直到现在还没有人要求它。)

同时,Flurl 很容易扩展,您可以通过扩展方法自己添加此支持。这应该是您流畅地发布 XML 字符串所需的全部内容:

public static class FlurlXmlExtensions
{
    // chain off an existing FlurlClient:
    public static async Task<HttpResponseMessage> PostXmlAsync(this FlurlClient fc, string xml) {
        try {
            var content = new CapturedStringContent(xml, Encoding.UTF8, "application/xml");
            return await fc.HttpClient.PostAsync(fc.Url, content);
        }
        finally {
            if (AutoDispose)
                Dispose();
        }
    }

    // chain off a Url object:
    public static Task<HttpResponseMessage> PostXmlAsync(this Url url, string xml) {
        return new FlurlClient(url, true).PostXmlAsync(xml);
    }

    // chain off a url string:
    public static Task<HttpResponseMessage> PostXmlAsync(this string url, string xml) {
        return new FlurlClient(url, true).PostXmlAsync(xml);
    }
}

更新:

我决定不在 Flurl 中包含 XML 支持,主要是因为社区成员站出来为它创建了一个很棒的扩展库:

https://github.com/lvermeulen/Flurl.Http.Xml

于 2015-03-10T21:04:11.860 回答
1

通过源代码挖掘,似乎 aSendAsync正在进行中。由于最新的包还不支持它,我想添加自己的:

public static class FlurlHttpExtensions
{
    /// <summary>
    /// Sends an asynchronous POST request of specified data (usually an anonymous object or dictionary) formatted as XML.
    /// </summary>
    /// <param name="client">The FlurlClient</param>
    /// <param name="serializedXml">Data to be serialized and posted.</param>
    /// <returns>A Task whose result is the received HttpResponseMessage.</returns>
    public static Task<HttpResponseMessage> PostXmlAsync(this FlurlClient client, string serializedXml)
    {
        var request = new HttpRequestMessage(HttpMethod.Post, client.Url.ToString())
        {
            Content = new CapturedStringContent(serializedXml, Encoding.UTF8, "text/xml"),
            Method = HttpMethod.Post
        };
        return client.HttpClient.SendAsync(request);
    }
}

是的,我采取了一些捷径,比如接受序列化的 XML 而不是序列化自己。目前,这对我有用。

如果有更好的方法,我会全力以赴!

于 2015-03-10T21:04:13.483 回答
1

我对此有点晚了,但是,我已经设法将 Flurl 与 Xml 一起使用(xml 扩展有问题)

首先,获取您的 xml 数据字符串,将其转换为 Html 内容

var httpContent = new StringContent(xmlDateString, Encoding.UTF8, "text/xml");

然后,在正常的 postasync 调用中使用该内容。

var response = await "http://localhost/Notices/webservices/importer.asmx"
        .WithHeader("Authorization", "Basic ssssserrrrr")
        .WithHeader("Content-Type", "text/xml")
        .WithHeader("SOAPAction", "\"http://tempuri.org/ImportData\"")
        .PostAsync(httpContent);

在此调用中,我正在设置内容类型。我也在设置肥皂动作(这是我正在调用的网络方法)。

然后它正常工作。

于 2017-12-05T11:49:01.757 回答
1

FlurlRequest 的修订版

public static class FlurlXmlExtensions
{
    // chain off an existing FlurlRequest:
    public static async Task<HttpResponseMessage> PostXmlAsync(this IFlurlRequest fr, string xml)
    {
        var content = new CapturedStringContent(xml, Encoding.UTF8, "application/xml");
        return await fr.PostAsync(content);
    }

    // chain off a Url object:
    public static Task<HttpResponseMessage> PostXmlAsync(this Url url, string xml)
    {
        return new FlurlRequest(url.Path).PostXmlAsync(xml);
    }

    // chain off a url string:
    public static Task<HttpResponseMessage> PostXmlAsync(this string url, string xml)
    {
        return new FlurlRequest(url).PostXmlAsync(xml);
    }
}
于 2020-08-21T21:37:30.340 回答
0

对于我的场景,我只需要发布我的 XML 的字符串表示形式,同时能够将 Content-Type 设置为text/xml并将编码设置为utf-8. 不确定是否有更简单的方法可以做到这一点,我只是没有看到,但这是一个让我到达那里的扩展方法:

public static class FlurlXmlExtensions
    {
        /// <summary>
        /// Sends an asynchronous POST request that contains an XML string.
        /// </summary>
        /// <param name="client">The IFlurlClient instance.</param>
        /// <param name="data">Contents of the request body.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation. Optional.</param>
        /// <param name="completionOption">The HttpCompletionOption used in the request. Optional.</param>
        /// <returns>A Task whose result is the received HttpResponseMessage.</returns>
        public static Task<HttpResponseMessage> PostXmlStringAsync(this IFlurlClient client, string data, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
        {
            var content = new CapturedStringContent(data, Encoding.UTF8, "text/xml");

            return client.SendAsync(HttpMethod.Post, content: content, cancellationToken: cancellationToken, completionOption: completionOption);
        }

    }
于 2017-03-17T17:44:45.603 回答