Home >Backend Development >PHP Tutorial >Why is My Android App Receiving HTML Instead of JSON from a ByetHost Server?

Why is My Android App Receiving HTML Instead of JSON from a ByetHost Server?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-28 05:16:02330browse

Why is My Android App Receiving HTML Instead of JSON from a ByetHost Server?

ByetHost Server Passing HTML Values "Checking Your Browser" with JSON String

Problem
When trying to parse a JSON string on Android, HTML values are returned instead. This sudden issue affects all pages hosted on the ByetHost server, while pages hosted on other servers work properly.

Solution

The issue arises due to the ByetHost server implementing the testcookie-nginx-module security anti-bot measure. This module performs a two-step validation process:

  1. For the first HTTP request, the module returns a JavaScript snippet instead of the expected JSON response. This snippet generates a validation cookie containing an AES key on the client-side.
  2. Subsequent HTTP requests with the generated cookie allow access to the desired JSON response.

Steps to Resolve the Issue for Android Apps

  1. Obtain the Cookie Key from a Web Browser:

    • Access the ByetHost URL in a web browser.
    • Check the browser's cookie settings and note the "__test" cookie.
    • Copy the values for "content," "path," and "expires" from the "__test" cookie.
  2. Set the Cookie on the Android App:

In the HTTP request code of your app, add a header below to include the "__test" cookie:

<code class="java">httpPost.addHeader("Cookie", "__test=THE_CONTENT_OF_YOUR_COOKIE_HERE; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/");</code>

Replace "THE_CONTENT_OF_YOUR_COOKIE_HERE" with the "content" value obtained in step 1.

With these changes, the Android app will include the "__test" cookie in its HTTP requests, allowing it to access the desired JSON responses from the ByetHost server.

The above is the detailed content of Why is My Android App Receiving HTML Instead of JSON from a ByetHost Server?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn