为什么此请求返回包含字节而不是JSON的字符串?

时间:2016-03-06 19:10:32

标签: java json http okhttp okhttp3

我正在使用okhttp库向返回JSON数据的某个端点发出GET请求。但是它返回一个带有字节而不是JSON的String类型。

以下是我正在尝试的内容:

<OTHER CLASSES REDACTED>

public static class GetDrivesRequest {
 private final OkHttpClient client = new OkHttpClient();

 public Response run(String offset, HashMap < String, String > headers) throws IOException {
  if (offset == null || offset == "") offset = "0";

  HttpUrl mUrl = new HttpUrl.Builder()
   .scheme("https")
   .host("www.waze.com")
   .addPathSegment("row-Descartes-live")
   .addPathSegment("app")
   .addPathSegment("Archive")
   .addPathSegment("List")
   .addQueryParameter("minDistance", "1000")
   .addQueryParameter("count", "50")
   .addQueryParameter("offset", offset)
   .build();

  Request.Builder requestB = new Request.Builder()
   .url(mUrl);

  for (Map.Entry < String, String > entry: headers.entrySet()) {
   String key = entry.getKey();
   String value = entry.getValue();

   if (key != "" && value != "") {
    requestB.addHeader(key, value);
   }
  }

  Request request = requestB.build();

  Response response = client.newCall(request).execute();
  return response;
 }
}

public static void main(String[] args) throws Exception {
  String GET_CSRF_URL = "https://www.waze.com/login/get";
  String SESSION_URL = "https://www.waze.com/login/create";
  String SESSION_LIST_URL = "https://www.waze.com/row-Descartes-live/app/Archive/List";
  String SESSON_DATA_URL = "https://www.waze.com/row-Descartes-live/app/Archive/SessionGPS";

  // ===[ 1 - Get CSRF token ]===============
  // ----------------------------------------
  GetCsrfRequest getCsrf = new GetCsrfRequest();
  Response req = getCsrf.run(GET_CSRF_URL);

  // ===[ 2 - Login ]===============
  // -------------------------------
  HashMap<String, String> cookies = new HashMap<String, String>();
  cookies.put("editor_env", "row");
  cookies.put("_web_session", req.headers().toString().split(":")[6].split("=")[1].split(";")[0]);
  cookies.put("_csrf_token", req.headers().toString().split(":")[9].split("=")[1].split(";")[0]);

  HashMap<String, String> headers = new HashMap<String, String>();
  headers.put("Accept", "*/*");
  headers.put("Accept-Encoding", "gzip, deflate");
  headers.put("Accept-Language", "nl,en;q=0.8,en-US;q=0.6,es;q=0.4,de;q=0.2,fr;q=0.2,it;q=0.2");
  headers.put("Connection", "keep-alive");
  headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  headers.put("Cookie", cookies.toString());
  headers.put("Host", "www.waze.com");
  headers.put("Origin", "https://www.waze.com/");
  headers.put("Referer", "https://www.waze.com/");
  headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
  headers.put("X-CSRF-Token", req.headers().toString().split(":")[9].split("=")[1].split(";")[0]);
  headers.put("X-Requested-With", "XMLHttpRequest");

  PostRequest postLogin = new PostRequest();
  Response req2 = postLogin.run(SESSION_URL, "<REDACTED>", "<REDACTED>", headers);

  // ===[ 3 - Get list of drives ]===============
  // --------------------------------------------
  HashMap<String, String> cookies2 = new HashMap<String, String>();
  cookies2.put("editor_env", "row");
  cookies2.put("_web_session", req2.headers().toString().split(":")[6].split("=")[1].split(";")[0]);
  cookies2.put("_csrf_token", req2.headers().toString().split(":")[9].split("=")[1].split(";")[0]);

  HashMap<String, String> headers2 = new HashMap<String, String>();
  headers2.put("Accept", "*/*");
  headers2.put("Accept-Encoding", "gzip, deflate");
  headers2.put("Accept-Language", "nl,en");
  headers2.put("Connection", "keep-alive");
  headers2.put("Content-Type", "application/json");
  headers2.put("Cookie", cookies2.toString());
  headers2.put("Host", "www.waze.com");
  headers2.put("Origin", "https://www.waze.com/");
  headers2.put("Referer", "https://www.waze.com/");
  headers2.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
  headers2.put("X-Requested-With", "XMLHttpRequest");

  GetDrivesRequest drivesRequest = new GetDrivesRequest();
  Response req3 = drivesRequest.run("0", headers2);
  System.out.println(req3.body().string());
}

这似乎返回一个填充了字节的字符串:

����n7��e�:�I��9����؋X�#�U@л���
�F��IR�9��js�����ϤN��Y�U`�`��\-$��čj�]���A�Y-�;
��8y�l5�����q��/4��|sxw��>��D��5r�2���<O!�-���]&g�2�vj����C���,�v��cM�0[�lD�uO���;��i�0�"��fR�>���ϭJ�ijf�zF  ������w��F��4FI�ԫV���1������
dGQ��8����56�4b���X6y�]8��cF)j8Ⱦ�p��    ĺ��N栙l�IT��K�-Y�����ϚA�1���c���K��1u�����
�E���?���8��P_��K�����y�Kֲ��3y
��J�3�oc�$��h#��Ls���k7��s�xp}Y)b�I�sၧ>���&���2=klƧ�t�1G����5p�q)`�BL��X�̎=�%�渖�{�k��8��g�V��Ê�ZwPPKEZp�+�o��ɨ�0cf�`/�d��&Cnjy�DB��}���Ƃ��f����Pw���W5��Z�0Y'��|�qCM8�}$�96����
wZ�q}�����.��]>���H������5�TL��{_{f�S�P�3;[���`ˤ) ��F30BP�_
BČn{
�;��g$�͌3b�/���<w
q��"�2�fU�5p�q�Şr����"�8��j�Ůт��S~��7��ol���:�������G#

我做错了吗? req.body().string()req2.body().string()会返回正确的数据。

0 个答案:

没有答案