CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
ArrayList<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("name", "zhangsan"));
params.add(new BasicNameValuePair("age", "23"));
//发送过去数据的格式
StringEntity entity = new UrlEncodedFormEntity(params, "utf-8");
post.setEntity(entity);
//设置消息头
post.setHeader(new BasicHeader("Content-Type", "application/json"));
post.setHeader(new BasicHeader("Accept", "application/json"));
post.setHeader(new BasicHeader("tenant", tenant));
HttpResponse response = httpClient.execute(post);
//entity就是返回过来的数据
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
JSONObject jsonObject = JSONObject.parseObject(result);
如何发送post请求,并获取响应过来的数据
最新推荐文章于 2024-04-14 21:09:52 发布