栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于HttpPost、CloseableHttpClient、CloseableHttpResponse实现网页的登录拿到cookie并抓取页面的数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于HttpPost、CloseableHttpClient、CloseableHttpResponse实现网页的登录拿到cookie并抓取页面的数据

基于HttpPost、CloseableHttpClient、CloseableHttpResponse实现网页的登录拿到cookie并抓取页面的数据

拿cookie

 //带request payload数据登录获取cookie
        String cookie="";
        String url="http://127.0.0.1:8080";
        HttpPost httpPost=new HttpPost(url);
        CloseableHttpClient httpClient= HttpClients.createDefault();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("","");
        StringEntity stringEntity=new StringEntity((JSONObject.toJSONString(jsonObject)),"application/json","utf-8");
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse closeableHttpResponse=httpClient.execute(httpPost);
        Header[] headers=closeableHttpResponse.getHeaders("Cookies");
        Pattern pattern=Pattern.compile("cookie(.*)");
        Matcher matcher=pattern.matcher(headers[0].toString());
        if(matcher.find()){
            cookie=matcher.group(0);
        }

抓取页面的数据,返回的类型为json

String url1="";
        HttpPost httpPost1=new HttpPost(url);
        httpPost1.addHeader("Cookie",cookie);
        JSONObject jsonObject1=new JSONObject();
        jsonObject1.put("",""); //请求参数
        StringEntity stringEntity1=new StringEntity((JSONObject.toJSONString(jsonObject1)),"application/json","utf-8");
        httpPost1.setEntity(stringEntity1);
        CloseableHttpResponse closeableHttpResponse1=httpClient.execute(httpPost1);
        HttpEntity httpEntity=closeableHttpResponse1.getEntity();
        InputStream inputStream=httpEntity.getContent();
        BufferedReader in=new BufferedReader(new InputStreamReader(inputStream));
        JSONObject responseData= JSON.parseObject(in.readLine());
        JSONArray jsonArray=responseData.getJSONArray("data"); //获取的json数组
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040928.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号