Arronlong / httpclientutil

该项目基于HttpClient-4.4.1封装的一个工具类。提供了更为简单的GET、POST及上传下载等功能。同时支持插件式配置Http-Header(包含自定义header)、配置SSL和Proxy等。
https://blog.csdn.net/xiaoxian8023/category_9264781.html
Apache License 2.0
1.41k stars 719 forks source link

关于cookies #28

Open wenbochang888 opened 5 years ago

wenbochang888 commented 5 years ago

如果我本地已经获取到了cookies,如何根据cookies来访问登陆的网站。 HttpConfig里面的context和header都设置过了,返回的仍然是没有登陆过的页面

Arronlong commented 5 years ago
  1. 通过restful等工具测试是否能通过,如果这个都通不过,那用程序也一样。
  2. 第一步测试通过,就此方式传参,设置cookie等,可以参考test包路径下的测试demo
wenbochang888 commented 5 years ago
  1. 通过restful等工具测试是否能通过,如果这个都通不过,那用程序也一样。
  2. 第一步测试通过,就此方式传参,设置cookie等,可以参考test包路径下的测试demo

通过用户名和密码登陆然后在获取cookie,放到context中就可以获取登陆后页面

但我拿到浏览器登陆过后的cookie,然后用python或者postman都是可以成功的,唯独在springboot中失败,包括设置其他参数。如referer,us-agent等

String cookies = "thi is cookie"; String referer = "https://www.baidu.com"; Header[] headers=HttpHeader.custom().cookie(cookies).referer(referer).build(); HttpConfig config = HttpConfig.custom().headers(headers); return config;

Arronlong commented 5 years ago

你可以参考test包中的2个demo:TestCookie、TestCookieWithHttpCookies。 https://github.com/Arronlong/httpclientutil/blob/master/src/test/java/com/arronlong/httpclientutil/test/TestCookieWithHttpCookies.java https://github.com/Arronlong/httpclientutil/blob/master/src/test/java/com/arronlong/httpclientutil/test/TestCookie.java