请求第三方接口

该代码段展示了如何使用Java原生的HttpClient进行GET请求,同时处理HttpServletRequest中的Cookie信息,并在请求中添加Header。如果用户已登录,将Cookie添加到请求中,否则使用默认的HttpClient。最后,从响应中获取数据并转化为JSONArray。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原生Http

 public static JSONArray doGet(String url, String param, HttpServletRequest request) {
        HttpClient client = null;
        JSONArray jsonArray = new JSONArray();
        HttpGet get = new HttpGet(param != null ? url + "?" + param : url);

		// 设置cookie -- 方法1(接收方接收不到)
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            boolean isLogin = false;
            String UUID = "";
            String UUID_NEW = "";
            String SSIONID = "";
            for (Cookie cookie1 : cookies) {
                if (cookie1.getName().equals("UUID") || cookie1.getName().equals("UUID_NEW")) {
                    isLogin = true;
                    if (cookie1.getName().equals("UUID")) {
                        UUID = cookie1.getValue();
                    } else if (cookie1.getName().equals("NEW")) {
                        UUID_NEW = cookie1.getValue();
                    } else if (cookie1.getName().equals("SSIONID")) {
                        SSIONID = cookie1.getValue();
                    }
                }
            }
            if (isLogin) {
                BasicCookieStore cookieStore = new BasicCookieStore();
                BasicClientCookie cookie_1 = new BasicClientCookie("UUID", EASEOK_UUID);
                BasicClientCookie cookie_2 = new BasicClientCookie("UUID_NEW", EASEOK_UUID_NEW);
                BasicClientCookie cookie_3 = new BasicClientCookie("SSIONID", JSESSIONID);
                cookie_1.setDomain("localhost");
                cookie_2.setDomain("localhost");
                cookie_3.setDomain("localhost");
                cookie_1.setPath("/");
                cookie_2.setPath("/");
                cookie_3.setPath("/");
                cookieStore.addCookie(cookie_1);
                cookieStore.addCookie(cookie_2);
                cookieStore.addCookie(cookie_3);
                client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
                // 设置cookie -- 方法2
                get.setHeader("Cookie", "UUID=" + UUID + ";UUID_NEW=" + UUID_NEW + ";SSIONID=" + SSIONID);
            } else {
                client = HttpClients.createDefault();
            }
        }

        try {
            get.addHeader("content-type", "text/xml");
            HttpResponse res = client.execute(get);
            String response = EntityUtils.toString(res.getEntity());
            System.out.println(response);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK && !response.equals("[]")) {
                jsonArray = JSONObject.parseArray(response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jsonArray;
    }

//调用
JSONArray jsonArray = HttpUtil.doGet("http://192.168.2.100/platform/api/webDisk/getSettingIp", null,request);

webflux

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值