• java后端调用接口Basic auth认证


    该方法接收一个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 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 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文本

    java中fastJSON解析复合-CSDN博客

  • 相关阅读:
    MySQL基本操作
    open cv快速入门系列---数字图像基础
    关于git hooks
    域控操作四:本地化统一壁纸切可随时更改
    Spring注解总结
    记参加 2022 Google开发者大会
    基于stm32单片机的输入捕获测量脉宽Proteus仿真
    Illuminate/22圆桌回顾:Web3互操作性的未来现已到来
    1010 Radix
    Android12开发之窗口模糊功能的实现
  • 原文地址:https://blog.csdn.net/hzp666/article/details/133940317