HttpClient加JSON保持登陆状态浏览知乎
来源:互联网 发布:游族网络工作的朋友么 编辑:程序博客网 时间:2024/06/06 00:21
保持着cookie,就意味着你保持着自己的登陆状态,这里省略了获取cookie的状态,我是用Chrome中的EditThisCooike
把其中的cookie信息导入到json文件中
像这样,然后提取cookie用json-lib,有个较坑的bug就是依赖包,我放张图,给大家看我的json包,有两个包不能用最新的,不然无法运行。
用chrome查看request的头部信息,来设置信息
最后贴上代码
public class zhihu { public static void main(String[] args) { RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();//设置浏览器模式 CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); try { HttpGet get = new HttpGet("https://www.zhihu.com/"); //如果把页面打印到控制台上,会有姓名,和进入主页等,如果不保持状态,会到输入账号密码的页面//根据chrome设置的头部 get.setHeader("Accept-Language","en-US,en;q=0.8"); get.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"); //json分析 String json = ReadFile("/Users/wang/Documents/cookie.json"); JSONArray jsonArray = JSONArray.fromObject(json); int size = jsonArray.size(); StringBuilder cookie = new StringBuilder(); for (int i=0;i<size;i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); //设置cookie cookie.append(jsonObject.get("name")); cookie.append("="); cookie.append(jsonObject.get("value")); cookie.append("; "); } System.out.println(cookie); get.setHeader("Cookie",cookie.toString()); HttpResponse response = client.execute(get); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity)); System.out.println(response.getStatusLine().getReasonPhrase()); } catch (IOException e) { e.printStackTrace(); } } //读取json文件的内容 public static String ReadFile(String Path) { String laststr = ""; File file = new File(Path); try { BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String str = null; while ((str = bufferedReader.readLine()) != null) { laststr = laststr + str; } bufferedReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return laststr; }}
进入成功结果
当我注释掉设置cookie的语句时再发送请求得道到
0 0
- HttpClient加JSON保持登陆状态浏览知乎
- iOS 保持登陆状态
- APEX session保持登陆状态
- APEX session保持登陆状态
- 知乎登陆脚本
- 模拟登陆知乎
- Android下httpclient保持登录状态
- HttpClient保持登录cookie状态,继续操作
- 【scrapy】模拟登陆知乎
- python---模拟登陆知乎
- python 模拟知乎登陆
- selenium模拟登陆知乎
- SSO单点登陆和保持状态
- cookie实现保持用户登陆状态
- iOS 利用token保持登陆状态
- iOS点击“我的”,保持登陆状态
- Android客户端如何登陆学校图书馆网站并保持登陆状态
- HttpClient 模拟登陆,保持会话并进行后续操作
- Zabbix 监控时间与系统时间不一配置
- 指针和const
- Ubuntu 设置XX-NET全局代理
- 蓝桥杯三个整数排
- lua 自学笔记2
- HttpClient加JSON保持登陆状态浏览知乎
- eclipse building workspace太慢或者 js出错问题解决
- 二叉树的建立(多种方法)
- 获取屏幕的宽高度
- GC 的混乱之处
- 想把图片当背景,但是其他控件背景设为透明色后,显示的是背景颜色的问题解决
- Ubuntu 15.04下安装fenghuangscanner报错及解决方法
- session入mysql
- php处理session跨域问题