我正在使用HttpRequestMessage / HttpResponseMessage发出发布请求。一切正常,我将响应放入长度正确的字符串中,但是如果我想显示原始字符串,它将仅返回一个问号字符(?),而不会返回其他任何字符,就像输出只是一/两个字符的长度。
我尝试将字符串解码为UTF8,仍然是相同的。
这是我的代码:
var targetUrl = new Uri("https://www.targetUrl.com/");
var postData = GetPostData;
var request = httpClientHelp.GetRequest(HttpMethod.Post, targetUrl, postData);
var response = Await httpRequestProcess.SendAsync(request);
var data = Await response.Content.ReadAsStringAsync();
Debug.WriteLine(data.Length); // Correct, i.e. 5686
Debug.WriteLine(data); // Just displays "?"
//。这是httpRequestProcess类中的SendAsync函数:
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage)
{
var response = await Client.SendAsync(requestMessage);
return response;
}
Here is the difference between the string content and its length
如果我复制那个问号,则输出是多组未知字符,例如:�
答案 0 :(得分:2)
由于@SachinVishwakarma,我得以解决此问题。标题上的默认编码为“ gzip,deflate,br”,如Fiddler中所示。我要做的就是用HttpClientHandler初始化HttpClient,该客户端的AutomaticDecompression属性针对所有压缩方法进行了优化。
示例:
var _httpHandler = new HttpClientHandler() { CookieContainer = new System.Net.CookieContainer(), AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.None };
var _httpClient = new HttpClient(_httpHandler);
谢谢你们帮助我!