如何使用 jsoup 维护可变的 cookie 和会话?
public boolean isGood(String path)
{
if (p != path)
{
good = false;
}
if (good)
{
try
{
Connection connection = Jsoup.connect(path);
Map<String, String> cookys = Jsoup.connect(path).response().cookies();
if (cookys != cookies)
cookies = cookys;
for (Entry<String, String> cookie : cookies.entrySet())
{
connection.cookie(cookie.getKey(), cookie.getValue());
}
Doc = connection.get();
good = true;
}
catch (Exception e)
{
rstring = e.getMessage().toString();
good = false;
}
}
else
{
try
{
Response response = Jsoup.connect(path).execute();
cookies = response.cookies();
Doc = response.parse();
good = true;
}
catch (Exception e)
{
rstring = e.getMessage().toString();
good = false;
}
}
return good;
}
这个方法不对。我试图找出一种方法,在不知道存在哪些 cookie 的情况下,能够处理 cookie 更改以及维护会话。
我正在为我的简单机器论坛编写一个应用程序,当您单击某些自定义行为时,它会更改其 cookie 配置。
但如果该应用程序在我的网站上表现良好,我将发布一个版本供其他人用于其他论坛。
我知道我正朝着正确的方向前进,但逻辑有点让我失望。
任何建议将不胜感激。
public boolean isGood(String path)
{
if (p != path)
{
good = false;
}
if (good)
{
try
{
Connection connection = Jsoup.connect(path);
Map<String, String> cookys = Jsoup.connect(path).response().cookies();
if (cookys != cookies)
cookies = cookys;
for (Entry<String, String> cookie : cookies.entrySet())
{
connection.cookie(cookie.getKey(), cookie.getValue());
}
Doc = connection.get();
good = true;
}
catch (Exception e)
{
rstring = e.getMessage().toString();
good = false;
}
}
else
{
try
{
Response response = Jsoup.connect(path).execute();
cookies = response.cookies();
Doc = response.parse();
good = true;
}
catch (Exception e)
{
rstring = e.getMessage().toString();
good = false;
}
}
return good;
}
This method is not right. What I'm trying to figure out is a way to without know what cookies will exist, be able to handle cookie changes as well as maintain sessions.
I'm writing an app for my simple machines forum, and it changes its cookie config as you click around for some custom behavior.
But if the app does well for my site, I was going to publish a version for others to use for other forums.
I know I'm heading in the right direction, but the logic is kind of kicking my butt.
Any advice would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码非常混乱。流程不合逻辑,异常处理也很糟糕。像
if (p != path)
和if (cookys != cookies)
这样的对象引用比较没有任何意义。要比较对象的内容,您需要使用equals()
方法。就这一点而言,我知道您希望在同一域上的一系列后续 Jsoup 请求中维护 cookie。在这种情况下,您需要基本上遵循以下流程:
这可以重构为以下方法:
可以用作
注意即将推出的 Jsoup 1.6.2 将附带新的
Connection#cookies(Map)
方法应该使for
循环每次都变得多余。This code is very confusing. The flow is illogical and the exception handling is bad. The object reference comparisons like
if (p != path)
andif (cookys != cookies)
makes no utter sense. To compare object's contents you need to useequals()
method instead.To the point, I understand that you want to maintain cookies in a bunch of subsequent Jsoup requests on the same domain. In that case, you need to basically adhere the following flow:
This can be refactored to the following method:
which can be used as
Note that the upcoming Jsoup 1.6.2 will come with a new
Connection#cookies(Map)
method which should make thatfor
loop everytime superfluous.+1对于BalusC
我改变了你的代码中的一些并且它对我有用,所以你从网站获取cookie并且只获取文档
+1 for BalusC
I changed some in your code and it works for me, so you get cookie from site and only than get Document