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:
Initial Request:
Subsequent Requests:
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:
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!