这个也难了我好久,网上没有找到对口的。
下面这个可以直接用,只需要修改成你自己的请求参数即可。
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String content=null;
// 创建Post请求
HttpPost httpPost = new HttpPost("你的接口地址");
//设置请求参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
NameValuePair cmd = new BasicNameValuePair("Cmd",reqBoday.getCmd());
NameValuePair from = new BasicNameValuePair("From",reqBoday.getFrom());
NameValuePair sfzh = new BasicNameValuePair("Sfzh",reqBoday.getSfzh());
parameters.add(cmd);
parameters.add(from);
parameters.add(sfzh);
//构建form表单实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
// 获取服务端响应的数据
content = EntityUtils.toString(response.getEntity(),
"UTF-8");
System.out.println(content);
return content;
}
return content;