如何接受包含引号的字符集 [英] How to accept a charset containing quotes
问题描述
ReadAsStringAsync 方法中有一个 错误,它阻止了 Content-Type 看起来像<代码>应用程序/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屋!