包含请求头 body
public static void main(String[] args) { // url String urlPath = url+"/bvcsp/v1/auth/login"; // 请求方式 HttpRequest request = HttpUtil.createRequest(Method.POST,urlPath); // body Map<String, String> map = new HashMap<>(); map.put("username","admin"); map.put("password_encrypted","6848d640671"); map.put("timestamp",DateUtil.currentSeconds()+""); // 请求头 request.header("contentType","application/json"); // body 转 toJSONString request.body(JSON.toJSONString(map)); HttpResponse response = request.execute(); // 返回参数 String result = response.body(); System.out.println(result); // 解析Json字符串 JSONObject jsonObject = JSON.parseObject(result); JSONObject data = jsonObject.getJSONObject("data"); String token = data.getString("token"); String timeout = data.getString("timeout"); System.out.println(token); System.out.println(timeout); }
包含请求头,不包含body
public static void main(String[] args) { // url String urlPath = url + "/bvcsp/v1/user/admin1"; // 请求方式 HttpRequest request = HttpUtil.createRequest(Method.POST, urlPath); // 请求头 request.header("Content-Type", "application/json"); request.header("Authorization", "Y3UrQ1VfYWRtaW4zK2"); HttpResponse response = request.execute(); // 返回参数 String result = response.body(); System.out.println(result); }
关于请求头问题
注意:不能使用【_】比如 :Authorization_Ys
HttpServletRequest ysRequest = ServletUtils.getRequest(); // 获取请求头参数 String ysToken = ysRequest.getHeader("Authorization-Ys"); if (StringUtils.isEmpty(ysToken)) { throw new ServiceException("获取token失败"); }