【问题标题】:Http Post - comunication failed - why?Http Post - 通讯失败 - 为什么?
【发布时间】:2019-12-05 10:13:23
【问题描述】:

我用 Java (Android Studio) 编写了一个 HTTP POST 以从 NodeRed 数据库中获取一些信息。

当我执行此代码时,不会出现任何错误。但是数据库创建了一个带有空字段的条目,并且应该给一个带有数据的 JsonArray 的响应只是一个空的 JsonArray。

有人看到错误了吗?如果我用 Postman 测试数据库,一切正常。不仅如此,我已经编写了一个不带任何参数的 GET,它也可以正常工作。

这是我的 POST 方法:

URL url = new URL("https://example-page.de/ExistingUser");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
String boundary = "===" + System.currentTimeMillis() + "===";
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Host", "example-page.de");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("Cache-control", "no-cache");
con.setUseCaches (false);
con.setDoInput(true);
con.setDoOutput(true);

//Create REQUEST content
DataOutputStream wr = new DataOutputStream(con.getOutputStream ());
PrintWriter writer;
String LINE_FEED = "\r\n";
String charset = "UTF-8";
String name = "EMail";
String value = "hans.wurst";
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
writer.append("Content-Type: application/json; charset=" + charset).append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(value);
writer.flush();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();

//Read Response
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
        content.append(inputLine);
}
in.close();
con.disconnect();

I only put in this Parameter in the body.

【问题讨论】:

  • 您能分享一下您在 Postman 中所做的事情以说“一切正常”吗?
  • 可以在编辑中找到链接。
  • 你能用普通的System.out 输出你给writer 的内容吗?让我们检查一下它是否真的包含您所期望的。

标签: java database http post http-post


【解决方案1】:

您似乎没有将表单数据部分添加到正确的输出流中。 您不需要创建另一个打印机。

DataOutputStream wr = new DataOutputStream(con.getOutputStream ());
//PrintWriter writer;
String LINE_FEED = "\r\n";
String charset = "UTF-8";
String name = "EMail";
String value = "hans.wurst";
wr.append("--" + boundary).append(LINE_FEED);
wr.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
wr.append("Content-Type: application/json; charset=" + charset).append(LINE_FEED);
wr.append(LINE_FEED);
wr.append(value);
wr.flush();
wr.append(LINE_FEED).flush();
wr.append("--" + boundary + "--").append(LINE_FEED);
wr.close();

【讨论】:

    【解决方案2】:

    首先感谢您的帮助!

    我试过了,但是没有用。 我们大学的服务器上有一个虚拟机。我们的 URL 通过服务器内部的默认 PORT 传递到我们的虚拟机。这可能是问题吗?那么也许正文的内容没有从第一个网络服务器内部传递到我们的第二个网络服务器?因为如果我使用查询参数或在标头内创建新的键值对,就没有问题。一切正常。

    但现在我遇到了另一个问题。当我使用查询参数执行 GET 请求时,我可以使用正常的 URL。但是当我尝试使用查询参数进行 POST 时,我需要使用服务器的本地 IP 并将我的手机连接到我大学的本地网络。这很令人困惑,我看不出有什么原因? 你有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多