Home  >  Article  >  Backend Development  >  Why Does My Android App Receive HTML Instead of JSON When Accessing a ByetHost Server?

Why Does My Android App Receive HTML Instead of JSON When Accessing a ByetHost Server?

DDD
DDDOriginal
2024-10-28 02:34:02622browse

Why Does My Android App Receive HTML Instead of JSON When Accessing a ByetHost Server?

ByetHost Server Passing HTML Values 'Checking Your Browser' with JSON String: Android App Issue

Problem:

While parsing JSON strings in an Android app, HTML values are being passed. This issue occurs when accessing a ByetHost server with PHP files, but not other servers.

Solution:

ByetHost employs a security module called testcookie-nginx-module that adds an extra layer of validation to HTTP requests. This module follows a two-step process:

  1. Initial Request: The first HTTP request is redirected to a script that generates a validation cookie containing an AES key.
  2. Subsequent Requests: The client sends the validation cookie in subsequent requests, which the module validates to allow access to the desired URL.

Android App Implementation:

To resolve the issue in the Android app, follow these steps:

  1. Retrieve the Validation Cookie: Use a web browser (e.g., Google Chrome) to access the desired URL.
  2. Get the Cookie Key from the Browser: Use Chrome's settings to navigate to "Content Settings" and search for "__test" under the website's cookies. Copy the values of "content," "path," and "expires."
  3. Add the Cookie to Android Requests: Modify your JSONfunctions class to add the validation cookie to HTTP requests:
<code class="java">try {
    if (post == "POST") {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(loginUrl);
        httpPost.setEntity(new UrlEncodedFormEntity(para));
        httpPost.setHeader("User-Agent", "Mozilla/5.0 ...");
        httpPost.addHeader("Cookie", "__test=" + cookieContent + "; expires=" + cookieExpires + "; path=" + cookiePath);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } else if (post == "GET") {
        HttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(para, "utf-8");
        loginUrl += "?" + paramString;
        HttpGet httpGet = new HttpGet(loginUrl);
        httpGet.addHeader("Cookie", "__test=" + cookieContent + "; expires=" + cookieExpires + "; path=" + cookiePath);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    }
}</code>

Note: Replace cookieContent, cookieExpires, and cookiePath with the values you obtained from the browser.

This modification will ensure that your Android app sends the validation cookie with every HTTP request, bypassing the initial HTML redirect and allowing it to retrieve the JSON data.

The above is the detailed content of Why Does My Android App Receive HTML Instead of JSON When Accessing 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