HttpClient post请求接口

先封装类
-----------------------------------封装-----------------------------------------------
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.util.EntityUtils;


public class HttpClient {

    public static String sendHttpRequest(String url,String parameter,String contentType){
        CloseableHttpClient client = HttpClients.createDefault();
        String respContent=null;
        HttpPost httpPost=new HttpPost(url);
        httpPost.setHeader("Content-Type",contentType);
        StringEntity entity=new StringEntity(parameter,"UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        try{
            CloseableHttpResponse response=client.execute(httpPost);
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity he=response.getEntity();
                respContent= EntityUtils.toString(he,"UTF-8");
            }


        }catch (Exception ex){
            ex.printStackTrace();
        }

        return respContent;
    }
}

 

-----------------------------------------------调用-----------------------------------------------

@RequestMapping(value = "test", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody
    JsonResult test(String cerNumber, String start, String end, String nocf) {
        List<GxSysAttendance> list = new ArrayList<GxSysAttendance>();
        GxSysAttendance gxSysAttendance = null;
        JSONArray jsonArray = null;
        JSONObject json=null;
        String statusCode=null;
        Map<String, String> param = new HashMap<String, String>();
        param.put("cerNumber", ******);                //参数名
        param.put("start", ******);
        param.put("end", *******);
        param.put("nocf", ******);
        String endpoint = "http://www.************.com/******/******";   //url
        JSONObject paramStr = JSONObject.fromObject(param);
        String result = HttpClient.sendHttpRequest(endpoint, paramStr.toString(), "application/json;charset=utf-8");                                         //调用封装类
        if (result != null ) {
            json= JSONObject.fromObject(result);
            int code = (int) json.get("code");
            if (code == 10000) {
                jsonArray = (JSONArray) json.get("data");                   //Json反序列化成对象
                statusCode = "1";
            }else {
                statusCode="0";
            }
        }
//        return result;
        return new JsonResult(statusCode, jsonArray, (String)json.get("msg"));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值