ByetHost 伺服器使用JSON 字串傳遞HTML 值「檢查您的瀏覽器」:Android 應用程式問題
問題:
在Android 應用程式中解析JSON 字串時,會傳遞HTML 值。當使用 PHP 檔案存取 ByetHost 伺服器而不是其他伺服器時,會發生此問題。
解決方案:
ByetHost 使用名為 testcookie-nginx-module 的安全模組,該模組新增了對 HTTP 請求進行額外的驗證。此模組遵循兩步驟流程:
Android應用程式實作:
要解決Android 應用程式中的問題,請依照下列步驟操作:
<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>
>將cookieContent、cookieExpires 和cookiePath 替換為您從瀏覽器取得的值。
此修改將確保您的 Android 應用程式發送每個 HTTP 請求的驗證 cookie,繞過初始 HTML 重定向並允許它檢索 JSON 資料。以上是為什麼我的 Android 應用程式在訪問 ByetHost 伺服器時收到的是 HTML 而不是 JSON?的詳細內容。更多資訊請關注PHP中文網其他相關文章!