Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#http客户端时,请求头必须仅包含ASCII字符?_C#_Dotnet Httpclient - Fatal编程技术网

使用c#http客户端时,请求头必须仅包含ASCII字符?

使用c#http客户端时,请求头必须仅包含ASCII字符?,c#,dotnet-httpclient,C#,Dotnet Httpclient,我正在使用HttpClient通过传递头进行POST调用,但在某个时间点,我得到一个错误,如下所示: Request headers must contain only ASCII characters. stacktrace为: at System.Net.Http.HttpConnection.WriteStringAsync(String s) at System.Net.Http.HttpConnection.WriteHeadersAsync(HttpHeaders heade

我正在使用
HttpClient
通过传递头进行POST调用,但在某个时间点,我得到一个错误,如下所示:

Request headers must contain only ASCII characters.
stacktrace为:

at System.Net.Http.HttpConnection.WriteStringAsync(String s)
   at System.Net.Http.HttpConnection.WriteHeadersAsync(HttpHeaders headers, String cookiesFromContainer)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
这是我的密码:

public HttpWrapper(string endpoint, Func<IDictionary<string, string>> NewHeader)
{
    _httpClient = new HttpClient();
    _httpClient.BaseAddress = new Uri(endpoint);

    if (NewHeader != null)
    {
        var headers = NewHeader();
        foreach (var header in headers)
        {
            _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
        }
    }
}
公共HttpWrapper(字符串端点,Func NewHeader) { _httpClient=新的httpClient(); _httpClient.BaseAddress=新Uri(端点); if(NewHeader!=null) { var headers=NewHeader(); foreach(标头中的var标头) { _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key,header.Value); } } } 是否需要使用
header.Value
来解决此问题?我在网上阅读,所以看起来我需要在这里使用Utf-8,但不确定如何正确使用它

更新

我今天得到了这样的头值,它抛出了相同的异常,因为
HttpUtility.HtmlEncode
没有对其执行任何操作。我也不确定这个字符是什么?你知道为什么会这样吗

我也不确定


您可以尝试将标题值编码为HTML:

_httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, HttpUtility.HtmlEncode(header.Value));

您的字符串Ergänzendes将是“
Erg&änzendes

我希望这可能对您的特定场景或其他类似情况有所帮助。 在我的例子中,我的标题包含非ASCII字符,因为其中一个标题值是通过创建sha256 HMAC生成的。这是我试图发送请求的服务的要求。 因此,为了将这些字符编码成ASCII,我必须做的就是:

//hashedSignature is the one containing NON ASCII charcters!
string MessageSignatureValue = System.Text.Encoding.ASCII.GetString(hashedSignature); 
httpClient.DefaultRequestHeaders.Add(MessageSignaturField, MessageSignatureValue);
我刚刚包括了一些重要的代码

所以,System.Text.Encoding.ASCII.GetString()方法成功了


同样,我希望它能有所帮助。

我已经这样尝试过了,但它没有给我那个值-
string str=“Ergänzendes”;string val=Convert.ToBase64String(Encoding.UTF8.GetBytes(str));控制台写入线(val)返回。这是正确的期望吗?它只是打印这个值
RXJnw6RuemVuZGVz
,所以内部
httpclient
会将这个base64编码的字符串
RXJnw6RuemVuZGVz
转换为它最初返回的方式?我的假设正确吗?嗯,它改变了原文。您可以尝试将字符串编码为HMTL。答案被更新,然后像这样打印-
Ergä;nzendes
。我不确定这是不是正确的方法?是的,这是正确的方法。浏览器将标题文本呈现为“Ergänzendes”。“为了在URI中使用非ASCII字符,您需要使用%hexcode语法对其进行转义(请参阅RFC 2396的第2节)。“从@justinezequeel如何做到这一点?以下是可接受的答案?这也是正确的方法吗@贾斯汀:如果被接受的答案对你有效,那就好了。我明白了。那么,被接受的答案和你的建议有什么区别呢?有什么想法吗?你确定这是正确的语法-
string MessageSignatureValue=System.Text.Encoding.ASCII.GetString(hashedSignature)?老实说,我从未使用过HttpUtility。所以,我没有一个具体的答案。但是,从文档中可以看出:“将字符串转换为HTML编码的字符串。”如果这对您有效,则不会有任何区别,除了我调用该方法的方式,它告诉您希望将某些内容明确地编码为ASCII。也许,更容易理解目的。只是一个想法。给你钱