Home  >  Article  >  Backend Development  >  Why is my Android app receiving HTML instead of JSON data from a ByetHost server, and how can I fix it?

Why is my Android app receiving HTML instead of JSON data from a ByetHost server, and how can I fix it?

DDD
DDDOriginal
2024-10-28 03:44:31635browse

Why is my Android app receiving HTML instead of JSON data from a ByetHost server, and how can I fix it?

Server-Side Cookie Validation with ByetHost

Issue

An Android app is parsing JSON data from a ByetHost server but receiving HTML values instead. This issue is encountered after previously functioning correctly.

Solution

ByetHost has implemented an anti-bot security module called testcookie-nginx-module. This module validates HTTP requests using a two-step process:

  1. Initial Request:

    • The module generates a JavaScript redirect that creates a validation cookie with an AES key.
    • It redirects the client to the actual JSON URL.
  2. Subsequent Requests:

    • The validation cookie is used to validate the AES key and allow access to the JSON data.

Cause of HTML Values:

When the Android app requests data from the ByetHost server, it lacks the necessary validation cookie. As a result, the server returns the JavaScript redirect, which is treated as HTML by the app.

Solution for Android App:

Obtain the Validation Cookie:

  1. Access the JSON URL once using a web browser like Chrome.
  2. Copy the value of the "__test" cookie from the browser's developer tools.

Set the Cookie in the Android App:

Add the following code to the HTTP request in your Android app:

<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 "__test=THE_CONTENT_OF_YOUR_COOKIE_HERE" with the actual cookie value.

PHP File Example:

<code class="php"><?php
// Database connection
$result = mysqli_query($con, "SELECT * FROM `pj_medionline_mst_stockist` ORDER BY `ID` ASC");

$response = array();
$posts = array();
while ($row = mysqli_fetch_array($result)) {
    $posts[] = array(
        'id' => $row["ID"],
        'stkcode' => $row["stkcode"],
        'stkname' => $row["ComName"],
        'operatorid' => $row["operatorid"],
        'password' => $row["Password"]
    );
}
$response['stokist'] = $posts;
print(json_encode($response));
?></code>

The above is the detailed content of Why is my Android app receiving HTML instead of JSON data from a ByetHost server, and how can I fix it?. 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