如何接受包含引号的字符集

时间:2018-06-12 15:00:14

标签: c# .net-core

有一个bug in the ReadAsStringAsync method可以阻止阅读类似foreach($CDNA_new as $t){ $match = "NA"; # Go through the specie array foreach ($csv_specie as $c){ # Find specie in string if (strpos($t, $c) !== FALSE ){ $match = $c; break; } } echo "----------------------". "<br \>\n"; echo '+'.$match. "<br \>\n"; echo '+'.$t. "<br \>\n"; # Work further with the values to retrieve gene ID using eSearch } 的内容类型。它看起来还没有框架修复它,但有没有解决办法?

1 个答案:

答案 0 :(得分:1)

在阅读回复之前,您需要删除引号:

var contentType = response.Content.Headers.ContentType;
if (contentType.CharSet?.Contains('"') == true) {
    contentType.CharSet = contentType.CharSet.Replace("\"", "");
}

这是我正在使用的完整DelegatingHandler:

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的原因。