gpt4 book ai didi

php - HttpURLConnection 从 .php 文件返回垃圾

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:01 26 4
gpt4 key购买 nike

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

    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 "";
}
}

问题是,如果我带上“sb”,它会返回奇怪的垃圾值。好像是下面的js代码

函数 toNumbers(d){var e=[];d.replace(/(..)/g,function(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=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("72355c05897edf080a57d7f54b23a51e ");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+";expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.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 响应来自何处。我已经检查过了,对于每个请求,它都会返回一个包含重定向 URL 的 html 响应。它在浏览器上工作,因为浏览器会自动呈现此 html 响应,然后重定向到 url。您也可以自己查看:转到此站点:http://requestmaker.com/并放置此网址:http://emgcall.byethost9.com/getData.php?ckattempt=1并发出获取请求。然后,您可以观察代码的实际响应。

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

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

谢谢。

关于php - HttpURLConnection 从 .php 文件返回垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com