如何使用 Java POST 并包含参数和原始请求正文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12666702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:45:34  来源:igfitidea点击:

How can I POST using Java and include parameters and a raw request body?

javaapache-commons-httpclient

提问by Data Skeptic

I am communicating with a web service that expects a POST parameter and also expect Request body. I have confirmed that such a POST request can be done using a REST Console I have, but I am unable to make such a request in Java using Apache libraries.

我正在与一个需要 POST 参数并且还需要请求正文的 Web 服务进行通信。我已经确认可以使用我拥有的 REST 控制台完成这样的 POST 请求,但是我无法使用 Apache 库在 Java 中发出这样的请求。

In the code below, I am able to POST to the web service, and it correctly receives the contents of the variable raw_body. If I uncomment the first of the two commented lines, the web service receives the "fname" parameter, but it no longer receives the body of the POST.

在下面的代码中,我能够 POST 到 Web 服务,并且它正确接收变量 raw_body 的内容。如果我取消注释两条注释行中的第一行,Web 服务会收到“fname”参数,但不再收到 POST 的正文。

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
...

HttpClient httpClient = new HttpClient();
String urlStr = "http://localhost:8080/MyRestWebService/save";
PostMethod method = new PostMethod(urlStr);
String raw_body = "This is a very long string, much too long to be just another parameter";
RequestEntity re = new StringRequestEntity(raw_body, "text/xml", "UTF-16");
//method.addParameter("fname", "test.txt");
//httpClient.getParams().setParameter("fname", "test.txt");
method.setRequestEntity(re);

How can I transmit both the parameter and the body?

如何同时传输参数和正文?

采纳答案by Matt Whipple

You could use the setQueryStringmethod to add the parameters to the URL that is being POSTed to. From a RESTful perspective I'd argue you should normally not be doing that, however, since a POST should represent a call to a resource and anything that would qualify for a query parameter should be included in the representation that is being transferred in the request body...or it should represent qualification of the resource itself in which case it should be part of the path that is posted to which could then be extracted by the controller using @PathVariable/@PathParam or something similar. So in your case you could also be looking for something like POST /MyRestWebService/files/test.txtor more fittingly a PUT if you're saving the resource and know the URI. The code on the server could pull the filename out from a URL pattern.

您可以使用该setQueryString方法将参数添加到要发布到的 URL。但是,从 RESTful 的角度来看,我认为您通常不应该这样做,因为 POST 应该表示对资源的调用,并且任何符合查询参数的条件都应该包含在请求中传输的表示中正文...或者它应该代表资源本身的资格,在这种情况下,它应该是发布到的路径的一部分,然后控制器可以使用@PathVariable/@PathParam 或类似的东西提取该路径。因此,在您的情况下,POST /MyRestWebService/files/test.txt如果您要保存资源并知道 URI ,您还可以寻找类似或更合适的 PUT 内容。服务器上的代码可以从 URL 模式中提取文件名。

回答by gigadot

You need to make a POST request using multipart-form. Here is the example:

您需要使用 multipart-form 发出 POST 请求。这是示例:

Apache HttpClient making multipart form post

Apache HttpClient 制作多部分表单发布

Alternatively, you can make a POST request with the content (parameters and files) encoded using application/x-www-form-urlencodedbut it is not recommended when you want to make a POST request with large content, like files.

或者,您可以使用编码的内容(参数和文件)application/x-www-form-urlencoded发出 POST 请求,但当您想要发出包含大内容(如文件)的 POST 请求时,不建议这样做。