[英]Java post http request (express) REST api
Having some troubles communicating with a Express REST api through Java. 通过Java与Express REST api通信时遇到麻烦。
A simple route which is online at: http://localhost:5555/test 在以下位置在线的简单路由: http:// localhost:5555 / test
router.post('/test', function (req, res, next) {
console.log("recived request");
res.sendStatus(200);
});
As you can see, this route doesn't do much tho, only for connection testing purpose. 如您所见,此路由并没有多大用处,仅用于连接测试目的。
Spending like hours searching but didn't find a good example yet. 花费了数小时进行搜索,但找不到一个很好的例子。
Still got this peace of code but got exception. 仍然得到了这种代码的安宁,但有异常。
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");
If someone knows a peace of code I could work, that would be great! 如果有人知道我能用代码和平,那将是很棒的!
Thanks 谢谢
Edit Server is running: 编辑服务器正在运行:
> node index.js
server running on port: 5555
connection open
Exception from Java: Java例外:
System.err: null
Your Express route is expecting a POST
and your code does a GET
request ( openConnection
). 您的Express路由需要
POST
而您的代码会执行GET
请求( openConnection
)。 Try to change it to get
an retry your operation. 尝试改变它来
get
一个重试操作。
Because your route accepts POST
method, you may have 2 options. 由于您的路线接受
POST
方法,因此您可能有2个选择。
Change your request method in Java code to POST
将Java代码中的请求方法更改为
POST
connection.setRequestMethod("POST");
Change your route accepts GET
更改路线接受
GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.