java请求http链接并接受返回值

@RequestMapping("/wtfk")
    public static String wtfk(@AuthenticationPrincipal UserManagerEntity user) {
        String accountType = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            String url = "http://127.23.23.121/rest/getAllAccount?accountNo="+user.getYhdm();
            HttpGet get = new HttpGet(url);
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(get);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String result = EntityUtils.toString(responseEntity); // responseEntity只能取一次
                System.out.println("响应内容为:" + result); // result = "{\"list\":[{\"id\":123,\"xm\":\"zs\",\"accountType\":\"xs\"}]}"
                HashMap hashMap = JSON.parseObject(result, HashMap.class);
                System.out.println("响应内容转map:" + hashMap);
                List<Map<String,String>> list = (List<Map<String, String>>) hashMap.get("list");
                System.out.println("map转list:" + list);
                accountType = list.get(0).get("accountType");
                System.out.println("accountType类型:" + accountType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return accountType;
    }

js直接打开http链接页面

window.open("http://127.29.23.140:8080/group/"+res.toLowerCase()+"/systemback");
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值