我刚刚开始使用 Jersey 框架,希望使用 Jersey 客户端发送 HTTP 请求,并将 content-type
设置为 application/x-www-form-urlencoded
。
下面的两个选项会产生两个略有不同的请求。
// webTarget is a WebTarget instance, already configured
Form form = new Form();
form.param("some-string", "some string");
Response response1 = webTarget.request().header("Content-Type", "application/x-www-form-urlencoded").post(Entity.form(form));
Response response2 = webTarget.request(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(Entity.entity("some-string=some string, MediaType.APPLICATION_FORM_URLENCODED));
第一个选项会产生如下所示的正文:
some-string=some+string
第二个选项会产生如下所示的正文:
some-string=some string
为什么会有差异?它们不应该是一样的吗?
最佳答案
要了解发生了什么,您首先需要了解处理(反)序列化的组件。对于序列化, MessageBodyWriter
使用 MessageBodyReader
进行反序列化用来。您可以阅读JAX-RS Entity Providers
了解更多信息.
也就是说,Form
和 String
有不同的 MessageBodyWriter
,即 FormProvider
和 StringMessageProvider
, 分别。前者url-encodes the values ,而后者does not .
关于java - 与 Jersey 客户端的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814535/