依赖jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
工具类
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
public static String doPostForJson(String url, String jsonParams){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
.setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type","application/json"); //
try {
String str = null;
StringEntity se = new StringEntity(jsonParams);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if ("200".equals(String.valueOf(response.getStatusLine().getStatusCode()))) {
HttpEntity entity = response.getEntity();
str = EntityUtils.toString(entity);
}
return str;
} catch (Exception e) {
e.printStackTrace();
return "post failure :caused by-->" + e.getMessage().toString();
}finally {
if(null != httpClient){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
调用
String locationUrl = "http://";
Map<String, String> map=new HashMap<String, String>();
map.put("date",alarmLocation.getCreateDate());
String param= JSON.toJSONString(map);
String httpDefGet = HttpUtil.doPostForJson(locationUrl,param);
list =JSON.parseArray(httpDefGet, AlarmLocation.class);
后台接口
@ResponseBody
@RequestMapping(value="/alarmLocationJsonp/alarmlistjsonp")
public List<AlarmInfo> sysAlarm(HttpServletRequest request){
List<AlarmInfo> alarmInfoList = null;
String date;
try {
// String json = request.getParameter("param"); //这是通过通过get方式去url 拼接的键值对,post方式取不到值。
request.setCharacterEncoding("UTF-8"); //返回页面防止出现中文乱码
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));//post方式传递读取字符流
String jsonStr = null;
StringBuilder result = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null) {
result.append(jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
reader.close();// 关闭输入流
JSONObject jsonObject = JSONObject.parseObject(result.toString());
date = jsonObject.getString("date");
alarmInfoList = alarmInfoService.sysAlarm(date);
} catch (Exception e) {
}
return alarmInfoList;
}