httpClient POST带参实现

依赖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;
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值