java后端调用接口Basic auth认证
headers.set("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("用户名" + ":" + "密码").getBytes()));log.info("返回结果:{}" ,result.getBody().toJSONString());log.warn("查询失败,url={}",url);//传递参数,
·
该方法接收一个JSON字符串参数phoneNum 内容: {"phone":"13712312312"} 然后解析参数中的手机号,作为data去调用URL接口,接收接口返回的复合JSON并解析,拿到想要的数据 public String queryUserResumeURLInfo(String phoneNum) { System.out.println("this is phoneNum: ======"+phoneNum); if(phoneNum.length() == 0){ log.info("手机号为空!" ); return null; } // 请求地址 String url = "访问认证的URL"; HttpHeaders headers = new HttpHeaders(); String mediaType = MediaType.APPLICATION_JSON_VALUE; headers.setContentType(MediaType.parseMediaType(mediaType)); headers.set("Accept", mediaType); // headers.set("Basic Auth", "uVJowCwUE08z2Hfw0aLXTZvZo66C19Rk"); headers.set("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("用户名" + ":" + "密码").getBytes())); Map<String, Object> map = new HashMap<>(); //传递参数,这里解析了传进来的JSON类型的手机号,并作为参数传给调用的接口 map.put("phone", phoneNum.substring(phoneNum.indexOf(":")+2,phoneNum.lastIndexOf("}")-1)); JSONObject json = new JSONObject(map); // System.out.println("this is json: ======"+json.toString()); HttpMethod method = HttpMethod.POST; try { ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, json, JSONObject.class); if (result != null && result.getBody() != null) { log.info("返回结果:{}" ,result.getBody().toJSONString()); JSONObject jsonObject = JSONObject.parseObject(result.getBody().toString()); String resumeDataInfo =jsonObject.getJSONArray("data").getJSONObject(0).getJSONObject("basicInfo").getString("ehrCandidateExternalLink"); // System.out.println("this is data:-------"+resumeDataInfo); return resumeDataInfo; } else { log.warn("查询失败,url={}",url); } }catch (Exception e){ log.error("查询发生异常,url={}",url,e); } return null; }
其中fastJSON解析复杂JSON文本
更多推荐
已为社区贡献3条内容
所有评论(0)