gpt4 book ai didi

java - HTTP GET 请求获取 HTML 页面代码而不是预期的 json 响应

转载 作者:行者123 更新时间:2023-12-01 18:14:01 27 4
gpt4 key购买 nike

/这是代码/

<html><head><meta http-equiv="Refresh" content="0; URL=http://www.fifacoinservice.com/api/v1"></head><body></body></html>
HTTP/1.1 400 Bad Request
Server: PipeGuard
Date: Mon, 15 Jun 2015 10:29:32 GMT
Content-Type: text/html
Content-Length: 605
Connection: close

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<h1>400 Bad Request</h1>
<p>Your browser sent a request that this server could not understand. Sorry for the inconvenience.<br/>
Please report this message and include the following information to us.<br/>
Thank you very much!</p>
<table>
<tr>
<td>URL:</td>
<td>http://_</td>
</tr>
<tr>
<td>Server:</td>
<td>5.254.102.236.r00t-services.net</td>
</tr>
<tr>
<td>Date:</td>
<td>2015/06/15 10:29:32</td>
</tr>
</table>
<hr/>Powered by PipeGuard</body>
</html>

这是我的电话

URL url = new URL("http://www.server.com/api/v1");
String query = "api_key=q5EicSKs027cjut7qhZk&method";

//make connection
URLConnection urlc = url.openConnection();

//use post mode
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);

//send query
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(query);
ps.close();

//get result
BufferedReader br = new BufferedReader(new InputStreamReader(urlc
.getInputStream()));
String l = null;
while ((l=br.readLine())!=null) {
System.out.println(l);
}
br.close();

最佳答案

您需要设置Accept header 。

urlc.addRequestProperty("Accept","application/json");

更新

您的服务器可能不支持 application/json 数据类型。您可以通过curl检查它。

curl -X POST  -H "Accept: application/json" --data 'api_key=q5EicSKs027cjut7qhZk&method' http://www.server.com/api/v1

关于java - HTTP GET 请求获取 HTML 页面代码而不是预期的 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30841398/

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