我正在使用 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. 5686Debug.WriteLine(data); // Just displays "?"//。以下是 httpRequestProcess 类中的 SendAsync 函数:public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage){ var response = await Client.SendAsync(requestMessage); return response;}如果我复制该问号,输出是多组未知字符,例如:� �
1 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
标头的默认编码是“gzip、deflate、br”,如 Fiddler 中所示。我所要做的就是使用 HttpClientHandler 初始化 HttpClient,该 HttpClientHandler 的 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);
谢谢你们帮助我!
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消