请求第三方接口

原生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

你可能感兴趣的:(JAVA,servlet,java)