Java post http 请求(express)REST api

标签 java api http post express

通过 Java 与 Express REST api 通信时遇到一些问题。

一条简单的路线,在线地址:http://localhost:5555/test

router.post('/test', function (req, res, next) {
  console.log("recived request");
  res.sendStatus(200);
});

如您所见,该路由并没有做太多事情,仅用于连接测试目的。

花了几个小时搜索,但还没有找到一个好的例子。

仍然得到了这种和平的代码,但出现了异常。

URL url = new URL("http://localhost:5555/test");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("test");

如果有人知道我可以工作的和平代码,那就太好了!

谢谢

编辑 服务器正在运行:

> node index.js
server running on port: 5555
connection open

Java 异常:

System.err: null

最佳答案

您的 Express 路由需要一个 POST,而您的代码会执行一个 GET 请求 (openConnection)。尝试将其更改为get重试您的操作。

关于Java post http 请求(express)REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36211704/

相关文章:

java - Spring MVC - 是否可以通过 GET 接收强类型请求对象?

java - Java 中的币安 margin 借贷 API

node.js - Shopify API如何生成发票、运输标签和运输 list ?

.htaccess - 仅允许通过 http 访问 robots.txt,其他通过 https 访问

http - web.config 跨 HTTP 和 HTTPS 重定向多个别名

c++ - 如何在 Restinio 中使用 POST 处理程序?

java - "getOnItemSelectedListener"方法不适用于微调器

java - 为什么Java不能切换原始long?

java - 如何在android中像Bootstrap一样制作步骤栏

python - Revit API 2015 Python : GetAllViewports() takes exactly 1 argument (0 given)