简体   繁体   English

从Java服务器发送POST请求(码头)

[英]Send POST request from Java server (jetty)

I am trying to send a POST request from a Java server (Jetty) to another server https://example.com/api/test with parameters userId , token and status . 我正在尝试使用参数userIdtokenstatus将来自Java服务器(Jetty)的POST请求发送到另一台服务器https://example.com/api/test

In Java I am invoking the method below: 在Java中,我正在调用以下方法:

sendNotification("https://example.com/api/test", "test1", "test", 200)

and I see on the logs that it successfully prints: 我在成功打印的日志中看到:

Sent request to https://example.com/api/test with userId test1 and token test

However, in the https://example.com/api/test logs I receive no request at all. 但是,在https://example.com/api/test日志中,我根本没有收到任何请求。

However, when I try the command below, I do receive the request perfectly: 但是,当我尝试下面的命令时,我确实收到了请求:

curl -H "Content-Type: application/json" -X POST -d '{"userId": "helloo@apptium.com", "token": "asdfasdf", "status": 200}' https://example.com/api/test

What is wrong? 怎么了?

Java method: Java方法:

private void sendNotification(String endpoint, String userId, 
                              String token, int status) throws IOException {
        URL url = new URL(endpoint);
        URLConnection con = url.openConnection();
        HttpURLConnection http = (HttpURLConnection)con;
        http.setRequestMethod("POST");
        http.setDoOutput(true);

        byte[] out = ("{\"userId\":\"" + userId + "\",\"token\":\"" + token + "\",\"status\":" + status + "}").getBytes(StandardCharsets.UTF_8);
        int length = out.length;

        http.setFixedLengthStreamingMode(length);
        http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        http.connect();
        try {
            log.warn("Sent request to " + endpoint + " with userId " + userId + " and token " + token);
            OutputStream os = http.getOutputStream();
            os.write(out);
        }
        catch(IOException e) {
            log.error(APImessages.ERROR_500);
        }
    }

您必须调用“ os.close()”以完成将请求正文发送到远程服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON发布请求Java不发送数据到服务器 - json post request java not send data to server 从 Android 到 java 服务器(码头)的 httpPost 请求:参数丢失? - httpPost request from Android to java server (jetty): parameters get lost? 从 Quarkus/Java 发送一个简单的 POST 请求 - Send a simple POST request from Quarkus/Java 向服务器发送 Post 请求 - Send Post Request to Server 从POST请求向Java中的外部GET请求发送自定义RequestBody - send custom RequestBody from POST request to an external GET request in java Java-使用HttpURLConnection发出POST请求并将图像发送到服务器 - Java - Using HttpURLConnection to make POST request and send image to server 无法使用Java将POST请求中的ArrayList发送到REST服务器 - Not able to send an ArrayList in a POST request to the REST server using Java 如何使用Java在远程服务器上使用Cookie发送POST请求 - How to send POST request with cookie on a remote server using java 如何从jetty服务器中的java servlet发送大(超过64k)http响应? - How do I send large (over 64k) http responses from a java servlet in a jetty server? Jetty不会使用Java EE项目从我的打字稿中的服务器发送.ts文件 - Jetty doesn't send .ts files from server in my typescript with java EE project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM