当前位置: 首页 > 知识库问答 >
问题:

如何在使用PostUrlEncodedAsync时从Content-Type中移除Charset=UTF-8

陆弘新
2023-03-14
flurlClient.ConfigureHttpClient(c => c.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"));

共有1个答案

夏飞跃
2023-03-14

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

https://stackoverflow.com/a/44543016/915414建议使用StringContent更改内容类型:

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=“”;

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);
    }
}
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将不会出现。

 类似资料:
  • 当我尝试将content-type设置为时,我会丢失边界,并在响应中得到一个错误: 添加UTF-8字符集的正确方法是什么?

  • 当内容类型不是text/html、text/plain或text/xml,而是application/x-www-form-urlencoded内容类型时,我很难理解如何设置字符集。 给出以下(简化的)javascript代码: 如果我没有显式设置编码, Firebug告诉我内容类型是"Application/x-www-form-urlencoded; charset=UTF-8"。 例如,如果

  • 问题内容: 测试golang网络应用程序时出现问题。在已部署的版本中,nginx位于应用程序的最前面,并进行显式设置,以便所有文本内容类型都附加一个字符集声明。 在测试中,我直接打了golang应用程序,这里的内容类型没有字符集。尝试提供类似d3这样的库时,这会导致问题: 由于golang未指定字符集,因此这些字符在chrome中呈现为: 使golang http服务器在HTTP标头上输出char

  • Content-Type实体头用于指示所述媒体类型的资源的。 作为响应,Content-Type标题告诉客户实际返回的内容的内容类型。浏览器在某些情况下会执行 MIME 嗅探,并不一定会遵循此标头的值; 为了防止这种行为,X-Content-Type-Options可以将标题设置为nosniff。 在请求(例如POST或PUT)中,客户端通知服务器实际发送了什么类型的数据。 Header type

  • 问题内容: 我有一个带有BOM的UTF-8编码文件,并且想要删除BOM。是否有任何Linux命令行工具可从文件中删除BOM? 问题答案: BOM是Unicode代码点U + FEFF;UTF-8编码由三个十六进制值0xEF,0xBB,0xBF组成。 使用bash,您可以创建带有特殊引号形式的UTF-8 BOM,该形式实现Unicode转义:。因此,使用bash,从文本文件的开头删除UTF-8 BO

  • Content-Type,内容类型,一般是指网页中存在的Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件,这就是经常看到一些Asp网页点击的结果却是下载到的一个文件或一张图片的原因。 HTTP content-type 对照表 文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Typ