Java Unirest 将带有空 JSON 的 POST 请求发送到本地主机上运行的服务器,但在将相同的请求发送到云服



我正在用Java构建一个代理,它必须使用规划器来解决游戏。我正在使用的计划器在云上运行作为服务,因此任何人都可以向其发送HTTP请求并获得响应。我必须向它发送一份包含以下内容的JSON{"domain": "string containing the domain's description", "problem": "string containing the problem to be solved"}.作为响应,我收到一个包含状态和结果的JSON,这可能是一个计划,也可能不是一个计划,这取决于是否有问题。

以下代码段允许我调用计划器并接收其响应,从正文中检索 JSON 对象:

String domain = readFile(this.gameInformation.domainFile);
String problem = readFile("planning/problem.pddl");
// Call online planner and get its response
String url = "http://solver.planning.domains/solve";
HttpResponse<JsonNode> response = Unirest.post(url)
.header("accept", "application/json")
.field("domain", domain)
.field("problem", problem)
.asJson();
// Get the JSON from the body of the HTTP response
JSONObject responseBody =  response.getBody().getObject();

这段代码运行良好,我没有任何问题。由于我必须在代理上做一些繁重的测试,所以我更喜欢在本地主机上运行服务器,这样服务就不会饱和(它一次只能处理一个请求(。

但是,如果我尝试向本地主机上运行的服务器发送请求,则服务器收到的 HTTP 请求的正文为空。不知何故,JSON 未发送,我收到包含错误的响应。

以下代码段说明了我如何尝试向本地主机上运行的服务器发送请求:

// Call online planner and get its response
String url = "http://localhost:5000/solve";
HttpResponse<JsonNode> response = Unirest.post(url)
.header("accept", "application/json")
.field("domain", domain)
.field("problem", problem)
.asJson();

为了测试,我之前创建了一个小的 Python 脚本,它将相同的请求发送到 localhost 上运行的服务器:

import requests
with open("domains/boulderdash-domain.pddl") as f:
domain = f.read()
with open("planning/problem.pddl") as f:
problem = f.read()
data = {"domain": domain, "problem": problem}
resp = requests.post("http://127.0.0.1:5000/solve", json=data)
print(resp)
print(resp.json())

执行脚本时,我得到了正确的响应,并且似乎将JSON正确发送到服务器。

有谁知道为什么会这样?

好的,幸运的是我已经找到了这个问题的答案(不要尝试在凌晨 2-3 点编码/调试,它永远不会正确(。似乎问题在于我指定了我期望从服务器获得的响应类型,而不是我试图在请求正文中发送给它的内容:

HttpResponse Response = Unirest.post(url( .header("accept">, "application/json"(...

我能够通过执行以下操作来解决我的问题:

// Create JSON object which will be sent in the request's body
JSONObject object = new JSONObject();
object.put("domain", domain);
object.put("problem", problem);
String url = "http://localhost:5000/solve";
<JsonNode> response = Unirest.post(url)
.header("Content-Type", "application/json")
.body(object)
.asJson();

现在,我在标题中指定要发送的内容类型。此外,我还创建了一个JSONObject实例,其中包含将添加到请求正文的信息。通过这样做,它适用于本地和云服务器。

尽管如此,我仍然不明白为什么当我调用云服务器时我能够得到正确的响应,但现在这并不重要。我希望这个答案对面临类似问题的人有所帮助!

最新更新