zoukankan      html  css  js  c++  java
  • http携参请求汇总

    put请求

    HttpEntityEnclosingRequestBase http = new HttpPut (url); Map<String, Object> parameter = new HashMap<>(); parameter.put("jsonStr", new JSONObject()); parameter.put("token", token); Map<String, Object> response = HttpUtils.sendRequest(http, parameter);
    public static JSONObject sendRequest(HttpEntityEnclosingRequestBase http, Map<String, Object> parameter) throws Exception {
    
        String jsonStr = MapUtils.getString(parameter, "jsonStr");
        if (StringUtils.isAnyEmpty(jsonStr)) {
          throw new RuntimeException("参数缺失!");
        }
    
    //    HttpPost httpPost = new HttpPost(urlPath);
        http.setHeader("Content-Type", "application/json;charset=UTF-8");
        String token = MapUtils.getString(parameter, "token");
        if (StringUtils.isNotEmpty(token)) {
          http.setHeader("Authorization",  token);
        }
        http.setEntity(new ByteArrayEntity(jsonStr.getBytes(StandardCharsets.UTF_8)));
        String result = execute(http, StandardCharsets.UTF_8.toString());
    
        return JSON.parseObject(result);
      }

    private static String execute(HttpEntityEnclosingRequestBase http, String responseCharset) {
    String result = null;

    try (CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = client.execute(http)) {
    HttpEntity httpEntity = response.getEntity();
    if (httpEntity != null) {
    result = EntityUtils.toString(httpEntity, responseCharset);
    logger.info("响应: {} ", result);
    }
    } catch (Exception e) {
    throw new RuntimeException("接口调用异常!");
    }
    return result;
    }
     
     post请求  
    public Map<String, Object> send(Map<String, Object> request, String urlSuffix, String token) throws Exception{ Map<String, Object> parameter = new HashMap<>(); parameter.put("jsonStr", JSON.toJSONString(request)); parameter.put("token", token); HttpEntityEnclosingRequestBase httpPost = new HttpPost(url); JSONObject jsonObject = HttpUtils.sendRequest(httpPost,parameter); return jsonObject.toJavaObject(Map.class); }

    Get携带参数token请求
      String result = HttpRequest.get (url)
                    .header (Header.CONTENT_TYPE, "application/json")
                    .header ("Authorization", token)
                    .body (JsonUtils.toJson (request))//表单内容
                    .timeout (20000)//超时,毫秒
                    .execute ().body ();
            Map<String, Object> response = JSONObject.parseObject(result);
    人这辈子没法做太多事情,所以每做一件事都要做到精彩绝伦。 因为,这就是我的宿命。人生苦短,你明白吗? 所以这是我为人生做出的选择
  • 相关阅读:
    IBM Thread and Monitor Dump Analyzer for Java解决生产环境中的性能问题
    ORACLE中的字符串替换 replce、regexp_replace 和 translate
    ORA-01654 索引 无法通过 表空间扩展
    HTML篇之CSS样式:<button></button>按钮变成超链接<a></a>的样式
    HTML里用如何包含引用另一个html文件 .
    java程序中实现打开 某个指定浏览器
    Oracle查询数据库中所有表的记录数
    getOutputStream() has already been called for this response解释以及解决方法
    oracle索引,索引的建立、修改、删除
    各种组件的js 获取值 / js动态赋值
  • 原文地址:https://www.cnblogs.com/junjun1578/p/14717787.html
Copyright © 2011-2022 走看看