如何接受包含引号的字符集 [英] How to accept a charset containing quotes
问题描述
ReadAsStringAsync方法中有一个错误,该错误会阻止Content-Type看起来像application/json; charset="utf-8"
被读取.似乎还没有针对它的框架修复程序,但是有任何解决方法吗?
There's a bug in the ReadAsStringAsync method that prevents a Content-Type looking like application/json; charset="utf-8"
from being read. It doesn't look like there's a framework fix for it yet, but are there any workarounds?
推荐答案
在阅读响应之前,需要删除引号:
Before you read the response, you need to remove the quotes:
var contentType = response.Content.Headers.ContentType;
if (contentType.CharSet?.Contains('"') == true) {
contentType.CharSet = contentType.CharSet.Replace("\"", "");
}
这是我正在使用的完整DelegatingHandler:
Here's the complete DelegatingHandler that I'm using:
public class StripCharSetQuotesHandler : DelegatingHandler
{
public StripCharSetQuotesHandler(HttpClientHandler innerHandler) {
: base(innerHandler)
{
// Nothing additional.
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
var response = await base.SendAsync(request, cancellationToken);
var contentType = response.Content.Headers.ContentType;
if (contentType.CharSet?.Contains('"') == true) {
contentType.CharSet = contentType.CharSet.Replace("\"", "");
}
return response;
}
}
您要确保删除的引号尽可能靠近HttpClientHandler
,这就是构造函数接受HttpClientHandler
而不是HttpMessageHandler
的原因.
You want to make sure that you're stripping the quotes as close to the HttpClientHandler
as possible, which is why the constructor accepts an HttpClientHandler
and not an HttpMessageHandler
.
这篇关于如何接受包含引号的字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!