Java HTTP请求无法发布

发布于 2025-02-09 04:46:47 字数 924 浏览 0 评论 0原文

我运行了一个Nodejs Web服务器,我想在我的Java程序中找到一个主体。

HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .GET()
            .header( "Content-Type", "text/plain;charset=UTF-8")
            .uri(URI.create("http://localhost:3000"))
            .POST(HttpRequest.BodyPublishers.ofString("Hello"))
            .build();
    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println("status:" + response.statusCode());
    System.out.println("response:" + response.body());

如果我删除此行:.post(httprequest.bodypublishers.ofstring(“ hello”))一切正常,我从服务器中得到响应,但是请求没有任何件。 使用此行,我将获得状态代码404,对客户的响应的正文是:

我如何将主体添加到请求中?

I have a nodejs web server running and I want to a body to the request in my java program.

HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .GET()
            .header( "Content-Type", "text/plain;charset=UTF-8")
            .uri(URI.create("http://localhost:3000"))
            .POST(HttpRequest.BodyPublishers.ofString("Hello"))
            .build();
    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println("status:" + response.statusCode());
    System.out.println("response:" + response.body());

If I delete this line: .POST(HttpRequest.BodyPublishers.ofString("Hello")) everything works fine, I get the response from the server, but there is no body to the request.
With this line I get status code 404 and the body of the response to the client is:
enter image description here

How can I add body to the request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2025-02-16 04:46:47

首先,尝试了解GET和POST HTTP方法之间的差异

https://wwww.w3schools.com/tags/tags/tags/ref_httpmethods.asp 文档中的一个很好的例子: https://docs.oracle.com/en/java/java/javase/11/docs/api/java.net.http/java/net/net/http/http/httpprequest.bodypuplishers.bodypublishers.htmll

 HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://example.com/"))
            .header("Content-Type", "text/plain; charset=UTF-8")
            .POST(BodyPublishers.ofString("some body text"))
            .build();

您也可以在这里看
https://wwwww.baeldung.com/java-9-http-client

First of all try understand differences between GET and POST HTTP methods
https://www.w3schools.com/tags/ref_httpmethods.asp

There is also a good example in docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html

 HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://example.com/"))
            .header("Content-Type", "text/plain; charset=UTF-8")
            .POST(BodyPublishers.ofString("some body text"))
            .build();

You can also look here as well
https://www.baeldung.com/java-9-http-client

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文