【问题标题】:How to remove charset=utf-8 from Content-Type when using PostUrlEncodedAsync使用 PostUrlEncodedAsync 时如何从 Content-Type 中删除 charset=utf-8
【发布时间】:2018-03-12 10:00:28
【问题描述】:

使用 Flurl 的 PostUrlEncodedAsync 发布数据时,会自动设置以下 Content-Type:

应用程序/x-www-form-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

【问题讨论】:

    标签: httpclient flurl


    【解决方案1】:

    我没有使用 Flurl,而是尝试使用 HttpClient 来实现相同的目标。那没用,所以我为 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 以查看它是如何工作的。它默认为一个字符集:

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

    你猜怎么着... PostUrlEncodedAsync 的核心是使用 StringContent。

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

    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;
        }
    }
    

    自定义字符串内容:

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多