java发送post form_data请求

stephen2017
  • 10
List headers = new ArrayList();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"));
headers.add(new BasicHeader("referer", "https://weibo.com/tv/billboard"));
headers.add(new BasicHeader("cookie", "SUB=_2AkMokY13f8PxqwJRmP0Xz27hZYR-wgrEieKezXysJRMxHRl-yT9kqlEttRB6AxGjmB_oIUP7hTf42y1NYgzmxwF3nr3X; YF-V-WEIBO-G0=b09171a17b2b5a470c42e2f713edace0; _s_tentry=-; Apache=375387657023.476.1607406605269; SINAGLOBAL=375387657023.476.1607406605269; ULV=1607406605393:1:1:1:375387657023.476.1607406605269:; XSRF-TOKEN=8e5cfPVMjH-y_FK637dfbXPr"));
HttpPost post = new HttpPost(url);
HttpClientBuilder httpClient = HttpClientBuilder.create().setDefaultHeaders(headers);
CloseableHttpResponse response = httpClient.build().execute(post);
HttpEntity httpEntity = response.getEntity();
ResponseBody = EntityUtils.toString(httpEntity, "utf-8");

这个是我写的一个post 请求,但是还有一个参数 form_data不知道放在哪
下面是form_data 相关数据

data: {"Component_Billboard_Billboardcategory":{},"Component_Billboard_Billboardlist":{"cid":"4418213501411061","count":20}}

这个是data 表单 放哪里合适呢?

回复
阅读 1.3k
2 个回答
post.setEntity(new StringEntity(data));

// 1、声明保存请求参数的list
List<NameValuePair> list = new ArrayList<NameValuePair>();
// 2、设置请求参数
liad.add(new BasicNameValuePair("参数1", "参数1的值"));
liad.add(new BasicNameValuePair("参数2", "参数2的值"));
liad.add(new BasicNameValuePair("参数3", "参数3的值"));
// 3、构造form表单的entity对象
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, UTF_8);
// 4、设置该对象到httpPost中
httpPost.setEntity(urlEncodedFormEntity);

在第二部设置请求参数,把你的data数据放进去就可以了。

你知道吗?

宣传栏