简体   繁体   English

Apache HttpClient正确处理cookie?

[英]Apache HttpClient proper handling of cookies?

I'm writing an application that can communicate with a website and do things such as login, post stuff, etc. 我正在编写一个可以与网站进行通信并执行诸如登录,发布内容等操作的应用程序。

Long story short, the application is working fine. 长话短说,该应用程序运行正常。 Although, I'd like to make it more efficient rather than doing a low-level cookie integration. 虽然,我想提高效率,而不是进行低级Cookie集成。

I'm currently using 我目前正在使用

post.setHeader("Cookie", "abc");

I'm well aware of the CookieStore method, I've been searching all over Google/Stackoverflow for help. 我非常了解CookieStore方法,一直在Google / Stackoverflow各处搜索帮助。 So the issue I'm having is, my cookies are predefined in my code, that's a problem because some of the generic keys in my cookie can expire. 所以我遇到的问题是,我的cookie是在代码中预定义的,这是一个问题,因为cookie中的某些通用密钥可能会过期。 The cookies have cached or generic generated keys, that are given out individually. 这些cookie具有cachedgeneric生成的密钥,它们分别给出。

I need to be able to go to the website, and store whatever cookie is given to me first. 我需要能够访问该网站,并存储任何给我的Cookie。 THEN begin executing my code to login. 然后开始执行我的代码登录。 As of right now, the cookies are null. 截至目前,Cookie为空。

How would I approach this? 我将如何处理? I want to be efficient, I was thinking of running a "dummy" execution on the website, and then copy over the given cookies to the low-level cookie storage. 我想提高效率,当时我想在网站上运行“虚拟”执行,然后将给定的Cookie复制到低级Cookie存储中。 (I feel there is a better way, that's why I'm hoping to get some input here) (我觉得有更好的方法,这就是为什么我希望在这里获得一些意见)

Figured it out, I used the BasicCookieManager only, and I had it query all the Cookies using List to view whether or not the cookies are active. 弄清楚了,我仅使用BasicCookieManager,并让它使用List查询所有Cookies,以查看Cookies是否处于活动状态。 Works flawlessly. 完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM