zoukankan      html  css  js  c++  java
  • HttpPost请求将带有数组json格式数据作为请求体传入的简单处理方法

    通过httpclient的post方法发送json参数进行接口测试。借鉴知乎上“云层”的提供的方法。

    链接:https://www.zhihu.com/question/30878548/answer/121149629

    public static String sendHttpPost(String url, String body) throws Exception {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "application/json");
    httpPost.setEntity(new StringEntity(body));
    CloseableHttpResponse response = httpClient.execute(httpPost);
    System.out.println(response.getStatusLine().getStatusCode() + "
    ");
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8"); 
    System.out.println(responseContent);
    response.close();
    httpClient.close();
    return responseContent;
    }

    其中httpPost.setEntity(new StringEntity(body));body部分是JSON数据格式,可以不是定长数据,可以根据需求定义成数组形式,如下:

    {
       "splineList" : 
       [
          {
             "Points" : 
             {
                "Pos" : [
                   {
                      "Xasis" : 1.66156307383845,
                      "Yasis" : 132.2036349238536
                   },
                   {
                      "Xasis" : 2.598720236773715,
                      "Yasis" : 186.8623164821516
                   },
                   {
                      "Xasis" : 3.598720236773715,
                      "Yasis" : 186.8623164821516
                   }
                ]
             }
          },
          {
             "Points" : 
             {
                "Pos" : [
                   {
                      "Xasis" : 4.66156307383845,
                      "Yasis" : 132.2036349238536
                   },
                   {
                      "Xasis" : 5.598720236773715,
                      "Yasis" : 186.8623164821516
                   }
                ]
             }
          },
          {
             "Points" : 
             {
                "Pos" : [
                   {
                      "Xasis" : 6.66156307383845,
                      "Yasis" : 132.2036349238536
                   },
                   {
                      "Xasis" : 7.598720236773715,
                      "Yasis" : 186.8623164821516
                   },
                   {
                      "Xasis" : 17.598720236773715,
                      "Yasis" : 186.8623164821516
                   },
                   {
                      "Xasis" : 27.598720236773715,
                      "Yasis" : 186.8623164821516
                   },
                   {
                      "Xasis" : 37.598720236773715,
                      "Yasis" : 186.8623164821516
                   }
                ]
             }
          }
       ]
    }

    其中,splineList有多个Points组成,Points又由多个XasisYasis坐标组成.

    Json格式解释和编解码开源库请参考:http://json.org/json-zh.html.

  • 相关阅读:
    [Ramda] allPass, propEq
    [Elm] Installing and setting up Elm
    [Node.js] Use nodejs-dashboard event loop delay with hrtime()
    [Node.js] Use Realm Object Database with Node.js
    [CSS] Manipulate Images Using CSS Filter and Blend Modes
    Android实现弹出输入法时,顶部固定,中间部分上移的效果
    [置顶] linux下让php支持mysql——寻找消失的mysql
    Conversion between json and object using SBJson lib
    Linux2.6内核--中断线被关闭的情况
    字符串、十六进制、byte数组互转
  • 原文地址:https://www.cnblogs.com/yueyangtze/p/11096198.html
Copyright © 2011-2022 走看看