如何在使用PostUrlEncodedAsync时从Content-Type中删除charset = utf-8

时间:2017-09-30 20:25:55

标签: httpclient flurl

使用Flurl的PostUrlEncodedAsync发布数据时,会自动设置以下内容类型:

应用程序/ x-WWW窗体-urlencoded;字符集= UTF-8

如何删除charset = utf-8部分?

我已经尝试过:

flurlClient.ConfigureHttpClient(c => c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"));

但它不起作用。建议来自:https://stackoverflow.com/a/44548514/915414

1 个答案:

答案 0 :(得分:0)

我尝试使用HttpClient实现相同的目标,而不是使用Flurl。那不起作用,所以我为Flurl创建了一个扩展方法。

https://stackoverflow.com/a/44543016/915414建议使用StringContent更改Content-Type:

var jobInJson = JsonConvert.SerializeObject(job);
var json = new StringContent(jobInJson, Encoding.UTF8);
json.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; odata=verbose");

var flurClient = GetBaseUrlForOperations("Jobs");

return await flurClient.PostAsync(json).ReceiveJson<Job>();

虽然这确实改变了Content-Type,但charset = utf-8仍然存在。

我反编译了System.Net.Http.StringContent以了解它是如何工作的。它默认为charset:

this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "text/plain" : mediaType)
{
     CharSet = encoding == null ? HttpContent.DefaultStringEncoding.WebName : encoding.WebName
};

猜猜是什么...... PostUrlEncodedAsync的核心是StringContent。

所以,我为Flurl创建了一个扩展方法,它使用了类似的StringContent实现,其中CharSet =&#34;&#34 ;;

PostUrlEncodedAsyncWithoutCharset:

public static class HttpExtensions
{
    public static Task<HttpResponseMessage> PostUrlEncodedAsyncWithoutCharset(this IFlurlClient client, object data, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
    {
        CapturedUrlContentCustom urlEncodedContent = new CapturedUrlContentCustom(client.Settings.UrlEncodedSerializer.Serialize(data));
        return client.SendAsync(HttpMethod.Post, (HttpContent)urlEncodedContent, new CancellationToken?(cancellationToken), completionOption);
    }
}

CapturedUrlContentCustom:

public class CapturedUrlContentCustom : CapturedStringContentCustom
{
    public CapturedUrlContentCustom(string data)
    : base(data, (Encoding) null, "application/x-www-form-urlencoded")
    {
    }
}

CapturedStringContentCustom:

public class CapturedStringContentCustom : CustomStringContent
{
    public string Content { get; }
    public CapturedStringContentCustom(string content, Encoding encoding = null, string mediaType = null)
        : base(content, encoding, mediaType)
    {
        this.Content = content;
    }
}

CustomStringContent:

public class CustomStringContent : ByteArrayContent
    {
        private const string defaultMediaType = "application/x-www-form-urlencoded";


        public CustomStringContent(string content)
      : this(content, (Encoding)null, (string)null)
        {
        }


        public CustomStringContent(string content, Encoding encoding)
      : this(content, encoding, (string)null)
        {
        }


        public CustomStringContent(string content, Encoding encoding, string mediaType)
      : base(CustomStringContent.GetContentByteArray(content, encoding))
        {
            this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "application/x-www-form-urlencoded" : mediaType)
            {
                CharSet = ""
            };
        }

        private static byte[] GetContentByteArray(string content, Encoding encoding)
        {
            if (content == null)
                throw new ArgumentNullException(nameof(content));
            if (encoding == null)
                encoding = Encoding.UTF8;
            return encoding.GetBytes(content);
        }
    }

现在,您可以调用PostUrlEncodedAsyncWithoutCharset,并且不会出现charset = utf-8。