如何在java中发送带有post参数的简单http post请求 [英] How to send simple http post request with post parameters in java

查看:4963
本文介绍了如何在java中发送带有post参数的简单http post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的代码示例来发送带有从表单输入获得的post参数的http post请求。
我找到了Apache HTTPClient,它有很多API和许多复杂的例子,但我找不到一个简单的例子,用输入参数发送http post请求并获得文本响应。

I need a simple code example of sending http post request with post parameters that I get from form inputs. I have found Apache HTTPClient, it has very reach API and lots of sophisticated examples, but I couldn't find a simple example of sending http post request with input parameters and getting text response.

更新:我对Apache HTTPClient v.4.x感兴趣,因为不推荐使用3.x.

Update: I'm interested in Apache HTTPClient v.4.x, as 3.x is deprecated.

推荐答案

使用Apache HttpClient的HTTP POST请求示例 v.4.x

HTTP POST request example using Apache HttpClient v.4.x

HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("param1", param1Value, ContentType.TEXT_PLAIN);
builder.addTextBody("param2", param2Value, ContentType.TEXT_PLAIN);
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
HttpResponse response = httpClient.execute(httpMethod);

这篇关于如何在java中发送带有post参数的简单http post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆