java如何给网站发送post请求_在Java中发送HTTP POST请求

更新答案:

由于一些类,在原来的答案,在Apache HTTP组件的较新版本已被弃用,我发布此更新。

顺便说一下,您可以访问完整的文档以获取更多示例here。

HttpClient httpclient = HttpClients.createDefault();

HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");

// Request parameters and other properties.

List params = new ArrayList(2);

params.add(new BasicNameValuePair("param-1", "12345"));

params.add(new BasicNameValuePair("param-2", "Hello!"));

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

if (entity != null) {

InputStream instream = entity.getContent();

try {

// do something useful

} finally {

instream.close();

}

}

原始答案:

我推荐使用Apache HttpClient。它更快更容易实现。

PostMethod post = new PostMethod("http://jakarata.apache.org/");

NameValuePair[] data = {

new NameValuePair("user", "joe"),

new NameValuePair("password", "bloggs")

};

post.setRequestBody(data);

// execute method and handle any error responses.

...

InputStream in = post.getResponseBodyAsStream();

// handle response.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值