美文网首页
httpclient 携带 token 及cookie方式;

httpclient 携带 token 及cookie方式;

作者: JD2017 | 来源:发表于2018-07-05 18:28 被阅读0次

    一、Json对象登录用户,获取token信息,遍历返回的

            HttpClientContext context = HttpClientContext.create();
            CloseableHttpClient httpClient = HttpClients.createDefault();
            JSONObject jsonResult = null;
            HttpPost method = new HttpPost(url);
            if (null != jsonParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(),
                        "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            // 返回:内容请求;
            CloseableHttpResponse httpResponse;
            try {
                httpResponse = httpClient.execute(method, context);
                logger.info("json对象:" + jsonParam + "\nurl:" + url + "\n");
                //获取返回的请求header信息;
                HeaderIterator iterator = httpResponse.headerIterator();  
                 while (iterator.hasNext()) {  
                     System.out.println("\t" + iterator.next());  
                 } 
                // 获取Cookies
                CookieStore cookieStore = context.getCookieStore();
                List<Cookie> cookies = cookieStore.getCookies();
                Iterator<Cookie> cookiesItr = cookies.iterator();
                // 遍历Cookies;
                while (cookiesItr.hasNext()) {
                    Cookie cookieImp = cookiesItr.next();
                    logger.info("Cookies列表:" + cookieImp.toString()); 
                    System.out.println(cookieImp.getValue());
                    if (cookieImp.getName().equalsIgnoreCase("session_token")) {
                        String Cookie = cookieImp.getValue();
                        logger.info("目标session_token=" + Cookie);  //
                    }
                }
    

    二、携带cookie 及 token 请求列表;

                JSONObject jsonResult = null;
            //create client
            CloseableHttpClient client = HttpClients.createDefault();
            //create context
            HttpClientContext context = HttpClientContext.create();
            HttpPost post = new HttpPost(url);
            if(jsonObject!=null){
                StringEntity entity = new StringEntity(jsonObject.toString(),"UTF-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                post.setEntity(entity);
            }
            post.addHeader("X-XSRF-TOKEN","897392cb-0bf3-4400-b475-5b4b1a52b38e");
            //存储result数据;
            Map<String, String> map = new HashMap<String, String>();
            String result = null;
                    BasicCookieStore basicCS1=setCookies("Cookie","access_token=eyJh..;");
                    context.setAttribute(HttpClientContext.COOKIE_STORE,basicCS1);
                   CloseableHttpResponse httpResponse = client.execute(post, context);
                    result  = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
    

    相关文章

      网友评论

          本文标题:httpclient 携带 token 及cookie方式;

          本文链接:https://www.haomeiwen.com/subject/kpopuftx.html