Java调用http接口(x-www-form-urlencoded)

Java调用http接口(x-www-form-urlencoded)_第1张图片

 

try {
    String tjInsertUserUrl = "http://114.113.145.5:8732/" + "golaxy/authorization/systemAdmin/user/v4?access_token=" + "AT-355007-73xqqb1s0NmJb9wufUx7XAtDU4j2Sfa9z00";

    PostMethod postMethod = null;
    postMethod = new PostMethod(tjInsertUserUrl) ;
    postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

   
    Map paramMap = new HashMap<>();
    //存放用户信息的map
    Map userMap = new HashMap<>();
    userMap.put("userName","lyTest22");
    userMap.put("passWord","123456");
    userMap.put("realName","");
    paramMap.put("user",userMap);

    List appList = new ArrayList<>();

    Map appMap1 = new HashMap<>();
    String [] roleId1 =new String[]{"1681a378c8e0271668f8db892adcc988"};
    appMap1.put("appId","auth_golaxy");
    appMap1.put("roleId",roleId1);

    Map appMap2 = new HashMap<>();
    String [] roleId2 =new String[]{"53eac0ee6a879bfdca03b4917c05fd10"};
    appMap2.put("appId","bda_public");
    appMap2.put("roleId",roleId2);

    appList.add(appMap1);
    appList.add(appMap2);

    paramMap.put("app",appList);
    System.out.println("打印请求参数" + paramMap);

    String strParam = JSONObject.toJSONString(paramMap);
    //参数设置,需要注意的就是里边不能传NULL,要传空字符串
    NameValuePair[] data = {
            new NameValuePair("jsonData",strParam)
    };

    postMethod.setRequestBody(data);

    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
    int response = httpClient.executeMethod(postMethod); // 执行POST方法
    String result = postMethod.getResponseBodyAsString() ;
    System.out.println(response);
    System.out.println(result);
} catch (Exception e) {
    // logger.info("请求异常"+e.getMessage(),e);
    throw new RuntimeException(e.getMessage());
}
 
  

借鉴自:Java调用http接口(x-www-form-urlencoded) - sunnyny - 博客园

Java调用http接口(x-www-form-urlencoded)_第2张图片

 

你可能感兴趣的:(java,http,开发语言)