【发布时间】:2021-04-16 21:04:57
【问题描述】:
我正在尝试使用下面的代码登录到一个 rest api,虽然 fiddler 可以解码响应,但它没有被 WebClient 解码。我正在使用一个扩展我在 SO 上找到的 WebClient 的类,它允许我访问响应。它也在下面。
using (WebClientWithResponse client = new WebClientWithResponse())
{
client.Headers.Add("cache-control", "no-cache");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("Authorization", basicAuth);
client.Headers.Add("Accept", "application/json, text/json, text/x-json, text/javascript, application/xml, text/xml");
client.Headers.Add("User-Agent", "MyAppName/21.1.1");
client.Headers.Add("Host", "login.company.com");
client.Headers.Add("Accept-Encoding", "gzip, deflate");
using (StreamWriter sw = new StreamWriter(client.OpenWrite("https://login.companyapi.com/services/oauth2/token", "POST")))
{
sw.Write(loginContent);
}
//string response = Encoding.UTF8.GetString(client.Response);
string response = Encoding.UTF8.GetString(Decompress(client.Response));
File.WriteAllText(".\\response.txt", response);
Console.WriteLine(response);
}
public class WebClientWithResponse : WebClient
{
// we will store the response here. We could store it elsewhere if needed.
// This presumes the response is not a huge array...
public byte[] Response { get; private set; }
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = base.GetWebResponse(request);
var httpResponse = response as HttpWebResponse;
if (httpResponse != null)
{
using (var stream = httpResponse.GetResponseStream())
{
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
Response = ms.ToArray();
}
}
}
return response;
}
}
因此,您可以在下面看到 Fiddler 显示的内容(单击解码按钮后)以及我在应用中收到的内容。
这是 Notepad++ 在加载保存的响应文件时的外观。
� ]�Ks�@�K8�
!@Ъ�>P@M�B��H@��ݕ�����C�9~��3��JA�T�0��!�+����{�g�u��^r���� 1��(YtU+���H��4JWs��";d�E�[]�4��������9o�,�G�Ǘ@��뺧=j��0 �IT��Jd�0E](R���.Ir����͒�V؝�(2�J叏��!�1��q�S=U�4��턔'�R ����"ײmϱ��,rF�I���T�Lp����t�لM�3�:��(�[g�:�x��}}*��p
我以前从来没有遇到过这个问题,我一生都无法弄清楚原因。
编辑:按 Marc Gravell 的要求添加响应标头
HTTP/1.1 200 OK
Date: Fri, 16 Apr 2021 19:33:23 GMT
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private
Set-Cookie: BrowserId=m6jRGp7qEeuWJ50fQL0HHA; domain=.company.com; path=/; expires=Sat, 16-Apr-2022 19:33:23 GMT; Max-Age=31536000
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-ReadOnlyMode: false
Content-Type: application/json;charset=UTF-8
Vary: Accept-Encoding
Content-Length: 368
EDIT2:Marc Gravell 告诉我它没有解压缩。结果证明是正确的。我添加了这个找到here的函数来解压响应。
public static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
【问题讨论】:
-
什么是内容类型、内容编码等——事实上,所有的响应头?很可能它要么没有应用解压缩,要么数据不是文本
-
@MarcGravell 谢谢!它没有解压。做出回答,我会将其标记为已接受。
-
看起来他们错过了编码标头来告诉你,不是吗?所以客户端不知道解压?
-
你为什么要调整 WebClient 而不是使用 HttpWebRequest / HttpClient(可能是后者)?解压是自动的。编码的检测也是如此,它在 WebClient 中设置为
Encoding.Default(除非另有说明 - 正如您提前了解;仍然没有自动检测)。 -
@Jimi 如果服务器包含正确的标头,则自动解压;它没有