简体   繁体   English

Java - 使用apache.http.client.methods.HttpPost发送HTTP参数

[英]Java - sending HTTP parameters with apache.http.client.methods.HttpPost

i am trying to send application/json data using httpclient also i want to send some parameter with http post. 我正在尝试使用httpclient发送application/json数据,我也想用http post发送一些参数。

How to do this when using apache.http.client.methods.HttpPost . 使用apache.http.client.methods.HttpPost时如何执行此apache.http.client.methods.HttpPost

Please can some one help me on this. 请有人帮我这个。

Best Regards 最好的祝福

I'm not sure if you can send post parameters and JSON at the same time, since the JSON string already will be the content of the request body. 我不确定你是否可以同时发送post参数 JSON,因为JSON字符串已经是请求体的内容。 You could try sending the query parameters as part of the URL, and create a regular StringEntity for your JSON: 您可以尝试将查询参数作为URL的一部分发送,并为您的JSON创建常规StringEntity

String jsonString = createMyJsonString();
HttpPost post = new HttpPost(urlWithQueryParams);
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(jsonString,"UTF-8")); 

If you're posting to a REST service, it's common to include the parameters identifying the resource in the URL path. 如果您要发布到REST服务,则通常在URL路径中包含标识资源的参数。 So if you have control over the end point, you could consider making the POST url independent of query/post parameters. 因此,如果您可以控制终点,则可以考虑使POST网址独立于查询/帖子参数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM