gpt4 book ai didi

java - Nodejs 服务器向 Java 返回 Http 响应代码 400

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:57 24 4
gpt4 key购买 nike

我有一个java代码,它向nodejs服务器发出http post请求。 Nodejs 服务器执行正确的请求代码并发送回响应。然而,java 代码正在接收 Http 响应代码 400。

这是我的 java 代码:

protected String httpPostRequest(URL postUrl, byte[] fields, String requestType) {
try {
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", requestType);
conn.setRequestProperty("Content-Length", String.valueOf(fields.length));
conn.setDoOutput(true);
conn.getOutputStream().write(fields);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char) c);
return sb.toString();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
//e.printStackTrace();
}
return "";
}

这是 Nodejs 代码的样子:

app.post("/test", function(req, res){
res.send("File Created");
//res.status(200)
res.end();
});

我尝试过 res.status(200).send("something"), res.send("something"), res.send("something").end();但我仍然收到响应代码 400。

编辑:详细错误:

java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:3000/test
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at controller.utils.http.AbstractHttpClient.httpPostRequest(AbstractHttpClient.java:30)
at controller.utils.http.HttpClient.callExternalUrl(HttpClient.java:35)
at model.Project.<init>(Project.java:103)
at controller.ProjectController.createProject(ProjectController.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at controller.AbstractApplicantzController.processRequest(AbstractApplicantzController.java:114)
at view.AbstractWebApi.doRequestAction(AbstractWebApi.java:206)
at view.AbstractWebApi.processRequest(AbstractWebApi.java:189)
at view.AbstractWebApi.doPost(AbstractWebApi.java:66)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

最佳答案

您不应自行设置 Content-Length header 。发生错误是因为 Content-Length 的值错误。

Java 将缓冲您发送的任何内容并为您设置该值。不要尝试自己设置。删除这一行应该可以使其工作:

javaconn.setRequestProperty("Content-Length", String.valueOf(fields.length));

关于java - Nodejs 服务器向 Java 返回 Http 响应代码 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55478790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com