简体   繁体   English

与PHP服务器的httpURLConnection给出不同的响应(URL在Web浏览器上有效)

[英]httpURLConnection to php server gives different response (url works on web browser)

The code requests HTTP connection to server that runs with php (phpMyAdmin). 该代码请求与使用php(phpMyAdmin)运行的服务器的HTTP连接。 It gives response with JSONObject. 它使用JSONObject给出响应。

It worked when I was testing in Asia, however it gives different response in the United States. 当我在亚洲进行测试时,它起作用了,但是在美国却给出了不同的响应。

With given url: 使用给定的URL:

target = "http://example.com/request.php?value=DDPS"

This works on web browser but in Android device it gives: 这适用于网络浏览器,但在Android设备中提供:

<html><body><script type="text/javascript" src="/cupid.js"></script><script>
.....path=/";location.href="http://example.com/request.php?value=DDPS
&ckattempt=1";</script></body></html>

This fails to create JSONObject which is obvious. 这无法创建显而易见的JSONObject。

What would be possible problem on this behavior? 此行为可能会出现什么问题?

I have set permission to server that allows access from all continents. 我已对允许从所有大洲访问的服务器设置了权限。

Here is my httpURLconnection code: 这是我的httpURLconnection代码:

    try{
            URL url = new URL(target);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setInstanceFollowRedirects(true);
            InputStream inputstream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream));
            String temp;
            StringBuilder stringBuilder = new StringBuilder();
            while((temp = bufferedReader.readLine()) != null)
            {
                stringBuilder.append(temp).append("\n");
            }

            bufferedReader.close();
            inputstream.close();
            httpURLConnection.disconnect();

            return stringBuilder.toString().trim();

        }catch (Exception e)            {
            e.printStackTrace();
        }

EDIT: 编辑:

HttpURLConnection.setFollowRedirects(true);

gives same behavior. 给出相同的行为。

Here is my php code for getting data from database and send response in JSON format. 这是我的php代码,用于从数据库获取数据并以JSON格式发送响应。 On which point should I implement file_get_contents? 我应该在哪一点实施file_get_contents?

<?php
header("Content-Type: text/html; charset=UTF-8");
$con = mysqli_connect("localhost", "exampleID", "examplePW", "exampleID");

$value = $_GET["title"];

$result = mysqli_query($con, "SELECT * FROM DB WHERE title = '$value'");

$response = array();

while($row = mysqli_fetch_array($result)){
    array_push($response, array("item1"=>$row[1], "item2"=>$row[2], "item3"=>$row[3], "item4"=>$row[4]));
}

echo json_encode(array("response"=>$response), JSON_UNESCAPED_UNICODE);
mysqli_close($con);
?>

probably issue with server redirects. 服务器重定向可能有问题。 Android gets the first response. Android得到了第一反应。 you could verify it with using a php scropt and file_get_contents method 您可以使用php scropt和file_get_contents方法进行验证

You can also try 您也可以尝试

HttpURLConnection.setFollowRedirects(true);

Solved. 解决了。

There was no issue in android codes. android代码中没有问题。
The issue was from the server I was connecting to. 问题出在我连接的服务器上。

It blocked some unknown IP addresses, so I needed to give permission to server to accept certain IP access. 它阻止了一些未知的IP地址,因此我需要授予服务器许可以接受某些IP访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Curl提供与普通浏览器不同的响应 - Curl gives a different response than normal browser javascript发布到.php文件一台服务器不起作用 - 在浏览器中手动输入URL到相同的.php工作 - javascript post to .php file one server does not work - manual entry of URL in browser to same .php works 通过PHP请求的网页返回404错误,但是当通过浏览器访问相同的URL时,它将起作用 - Web page requested through PHP returns a 404 error but when the same URL is accessed through a browser it works URL在浏览器中工作正常,但使用ajax请求给出404错误 - URL works Fine in browser but gives 404 error with ajax request 使用php的apk下载提供了未定义的功能,但在浏览器刷新后下载即可 - Apk download with php gives undefined but download works after browser refresh 用浏览器/邮递员,PHP卷曲不同的响应 - Curl different response with browser/postman , PHP 如何设置 php web 与不同浏览器的兼容性? - How to set php web compatibility with different browser? php-cURL响应标头与浏览器响应标头不同 - php - cURL response header different than browser response header PHP问题(在localhost上有效,但在Web服务器上错误) - Problem with PHP (works on localhost, but errors on web server) PHP登录可以在XAMPP上使用,但不能在Web服务器上使用? - php login works on XAMPP but not web server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM