您现在的位置是:首页 > 文章详情

http post带参数请求

日期:2018-04-24点击:508
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80073586
import java.util.ArrayList; import java.util.List;  import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;  public class DoPOSTParam { public static void main(String[] args) throws Exception { // 创建Httpclient对象  CloseableHttpClient httpclient = HttpClients.createDefault();   // 创建http POST请求  HttpPost httpPost = new HttpPost("http://www.oschina.net/search");   // 设置2个post参数,一个是scope、一个是q  List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);  parameters.add(new BasicNameValuePair("scope", "project"));  parameters.add(new BasicNameValuePair("q", "java"));  // 构造一个form表单式的实体  UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);  // 将请求实体设置到httpPost对象中  httpPost.setEntity(formEntity);   CloseableHttpResponse response = null;  try { // 执行请求  response = httpclient.execute(httpPost);  // 判断返回状态是否为200  if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(response.getEntity(), "UTF-8");  System.out.println(content);  } } finally { if (response != null) { response.close();  } httpclient.close();  } } } 
原文链接:https://yq.aliyun.com/articles/653103
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章