• Java:http的post请求(带参数json格式)示例代码


    Java:http的post请求(带参数json格式)示例代码:

    
    
    import cn.hutool.core.collection.CollectionUtil;
    import cn.hutool.core.util.StrUtil;
    import cn.hutool.http.HttpRequest;
    import cn.hutool.http.HttpResponse;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;


    public
    JSONObject sendPost(String url, LoginSsoReq req){ HttpResponse httpResponse = null; try { JSONObject json=new JSONObject(); json.put("client_id",req.getClientId()); json.put("grant_type",req.getGrantType()); json.put("code",req.getCode()); json.put("remote_ip",req.getRemoteIp()); json.put("redirect_uri",req.getRedirectUri()); // 设置请求头 Map<String, String > heads = new HashMap<>(); heads.put("Content-Type", "application/json;charset=UTF-8"); httpResponse = HttpRequest.post(url) // url .headerMap(heads, false) // 请求头设置 .body(json.toJSONString()) // json参数 .timeout(5 * 60 * 1000) // 超时 .execute(); // 请求 log.info("获取返回服务器的状态码:----- " + httpResponse.getStatus() ); if(httpResponse.getStatus() == 200){ //成功后响应数据 String result = httpResponse.body(); JSONObject jsonResult = JSONObject.parseObject(result); log.info("接口返回数据:"+jsonResult.toString()); return jsonResult; } } catch (Exception e) { e.printStackTrace(); } finally{ try { //释放连接 if(httpResponse != null){ httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } } return new JSONObject(); }
  • 相关阅读:
    打印机常识
    网络禁用和启用,及禁止软件软件访问网络
    局域网高级共享改写
    电脑桌面搬家
    电脑硬件的基本组装
    c#中Linq查询语句
    c#中Lamdba匿名函数查询语句
    C#中静态和非静态的区别
    c#中的面向对象
    Laravel 表单验证规则:required、present、filled 和 nullable
  • 原文地址:https://www.cnblogs.com/lizm166/p/16143017.html
Copyright © 2020-2023  润新知