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.
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 for BalusC
I changed some in your code and it works for me, so you get cookie from site and only than get Document