为什么 REST API 的响应在我的代码中不是 JSON 格式,而是 Google 扩展名 "restman" 的正确格式?

Why is the response of REST API not in JSON format in my code, but is in the correct format with Google extension "restman"?

为什么 REST API 的响应在我的代码中不是 JSON 格式?但是 Google 扩展名“RestMan”是正确的格式。我的代码是这样的:

using (WebClient client = new WebClient())
{
  byte[] response = client.UploadValues("http://personnel.fasau.ac.ir:8085/PersonApi/api/Token/Login", new NameValueCollection()
  {
    {"Username", "name"},
    {"Password", "pass"}
  });
string result = System.Text.Encoding.UTF8.GetString(response);
}

我得到的回复是这样的:

\u001f�\b[=15=][=15=][=15=][=15=][=15=]\u0004��n�P\u0010�_�b�D\�#�3�`��1\u001c\u000e\aÎ�[.\a�\b\u000e6Q\u0017ݴY�=��*+}\u0018��Lq��f4��#���0$�i�<6�a7f�>ߕ�#�;�I���M�kN�q0՞�\u0012��\�jy�%s]�\u001fD\X�c�Oa�6�=,\v�À(\u000e�ׁbՖ[�إ+ª�K�銕*�A\u001b�t�/��+Z�pPe\u0003\t�\u0005h �w\u0006�:TX��I�e��a��JR;\u000e�C%Yc\"/\u0093T�*�\u0002Jr��ҥ2@%��lv2�X�\u0001R1�8䴓��Ǎ�8\u0001�]�ru�hD��\u000fv�`ċ.��f�Gm�� �u=����\u001f�\u0004#N��\f�!�z\u001b\u0017�\u0001�uCa��\\a�\u0016��B\u0015���it�\u001f��K7\u0019�\r%\u0017�\u0012�\u0006����Ln�\r}��\u0011\a��G��iબ��0����\u0004C\u0001�p��\u0004�\u0016�<Z�.�}v]���\u001e \u0014*������\u0012>��u�x��J��6$�������ML�m��T�7�S�3\u0019��>�\u001a`<��\u00193�R\u0018\u0014�������_���2���o����\_����\a���=�X\u000e�1\u0003�z_㴡7[��?_���?n����u�/\u007f\u0001b�Q�S\u0002[=15=][=15=]

“RestMan”的响应体是这样的(格式正确):

{
    "Valid": true,
    "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6Im56RDhteWJaUkVkV2Fsdmppa2hCM0Y5RHdnQWMyRm5UczZja3ZMUjNiUkxFaTUwUGNrYUt3VTdoZHpKbS9KM1FlYkR0ZDhkZUlSZjBFMHdHcUJJRE01VVRld1JDS3pZOWU0akxKMzhBSmY3MVBQaU9RdG50YXNzYlV0Q0pRK2dCUlJ1a01oQTBUKzhFWnkvenNkQ0NUbTYzR01GTFR2S2NwOGtLUVh0ZmJ1WT0iLCJEaXNwbGF5TmFtZSI6IlciLCJuYmYiOjE2MDk2ODI3NDksImV4cCI6MTYwOTY5MzU0OSwiaWF0IjoxNjA5NjgyNzQ5LCJpc3MiOiJCcHRzb2Z0LmNvbSIsImF1ZCI6IkJwdHNvZnQuY29tIn0.4J0h30Gd1RzWl3tBulGWEQwcQW_PnTV0VTDFAb8S-vg",
    "FullName": "کاربر وب سرویس",
    "Photo": null,
    "ErrorTitle": "دانشگاه فسا"
}

尝试 unicode 编码:

string result = System.Text.Encoding.Unicode.GetString(response);

尝试设置 accept header,

client.Headers.Add("Accept", "application/json");

更新: 也有可能使用Gzip 压缩对响应进行了压缩。首先使用辅助函数解压缩字符串,然后使用 JSON 序列化程序对其进行序列化。

我用来在我的代码中解压缩 Gzip 的辅助函数,

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public string DecompressString(string s, Encoding encoding)
{
  // obtaining bytes from the string in the below step
  // In my case, it was a base 64 string
  // If not use encoding.GetBytes() function
  var bytes = Convert.FromBase64String(s); 

  using (var msi = new MemoryStream(bytes))
  using (var mso = new MemoryStream())
  {
    using (var gs = new GZipStream(msi, CompressionMode.Decompress))
    {
      gs.CopyTo(mso);
    }
    return encoding.GetString(mso.ToArray());
  }
}
var originalRes = // call your function here
var decompressedRes = helper.DecompressGzipString(originalRes, Encoding.UTF8);