HttpURLConnection正在从.php文件返回垃圾


HttpURLConnection is returning garbage from a .php file

我一直在尝试从.php文件中获取JSON数据。它正在返回垃圾值。但如果我把url放在浏览器中,它会完美地向我显示json数据。这是的代码片段

    String authString = username + ":" + password;
    byte[] authEncBytes = Base64.encode(authString.getBytes(),0);
    String authStringEnc = new String(authEncBytes);
    URL url = new URL(urlString);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestProperty("Content-type", "application/json");
    httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
    httpConn.setRequestMethod("GET");
    httpConn.connect();
    InputStream stream = httpConn.getInputStream();

并将其从输入流转换为字符串

public static String convertinputStreamToString(InputStream ists)
        throws IOException {
    if (ists != null) {
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            BufferedReader r1 = new BufferedReader(new InputStreamReader(
                    ists, "UTF-8"));
            while ((line = r1.readLine()) != null) {
                sb.append(line).append("'n");
            }
        } finally {
            ists.close();
        }
        return sb.toString();
    } else {
        return "";
    }
}

显而易见的是,如果我带了"某人",它会返回奇怪的垃圾值。看起来像js代码,就像下面的

函数toNumbers(d){var e=[];d.replace(/(..)/g,函数(d){e.push(parseInt(d,16))});return e}函数toHex(){for(var d=[],d=1==arguments.length&&arguments[0]。constructor==Array?arguments[0]:arguments,e=",f=0;fd[f]?"0":")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumerics("98344c2eee86c3994890592585b49f80"),c=toNumberes("72355c058989 7edf080a57d7f54b23a51e");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+";过期时间=12月31日星期四-37 23:55:55 GMT;路径=/";document.cookie="referrer="+转义(document.referrer);位置。href="http://emgcall.byethost9.com/getData.php?ckattempt=1";此网站需要Javascript才能工作,请在浏览器中启用Javascript或使用支持Javascript的浏览器

我已经尝试在php文件中将内容设置为Application/Json。同样的结果。

问题和解决方案可能是什么。

这是php代码

    <?php
    header('Content-type: application/json');
    function x(){
    $con=mysqli_connect("com","","","");
    if (mysqli_connect_errno($con)){
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql = "SELECT * FROM emgCall";
    $result = mysqli_query($con,$sql);
    if (mysqli_num_rows($result) > 0) {
        $response["numbers"] = array();
        while($row = mysqli_fetch_assoc($result)){
            $number = array();
            $number['id']=$row['id'];
            $number['number']=$row['number'];
            $number['image']=base64_encode($row['image']);
            array_push($response["numbers"], $number);
        }
        $son=json_encode($response);
        mysqli_close($con);
        return $son;
    } else {
        $outputs["success"] = 0;
        $outputs["message"] = "No products found";
    }
  }
  echo $data = x();
   ?>

首先,您需要检查这些html响应的来源。我已经检查过了,对于每个请求,它都会返回一个html响应,其中包含一个重定向url。它在浏览器上工作,因为浏览器会自动呈现这个html响应,然后重定向到url。你也可以自己检查:转到这个网站:http://requestmaker.com/并放置此网址:http://emgcall.byethost9.com/getData.php?ckattempt=1并提出获取请求。然后,您可以观察代码中的实际响应。

因此,请检查是否有任何添加到php服务器的模块或服务自动添加了一些cookies/auth数据,然后强制浏览器重定向。

我假设你的网址是:http://emgcall.byethost9.com/getData.php?ckattempt=1

谢谢。