Library for Android to use Jsoup on web pages with Cloudflare
Just add you have to add these lines in your build.gradle file:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
...
}
implementation 'com.github.Kelvao:CloudflareScrape:0.2.0'
Instantiate a new CloudflareScrap object and set the Builder:
CloudflareScrape cloudflareScrape = new CloudflareScrape.Builder()
.setURL("your url")
.setCallback(this) //implement wherever you want
.build();
And just execute the AsyncTask only once (probably in your "onCreateView"):
cloudflareScrape.execute();
The callback returns the cookie to use in your Jsoup Task:
@Override
public void CloudflareScrapedCookies(HashMap<String, String> hashMap) {
//Add your AsyncTask to Jsoup
}
Your AsyncTask to Jsoup will look like this:
//Send the cookies to your constructor or as you prefer
private HashMap<String, String> cookies;
public JsoupTask(HashMap<String, String> cookies) {
this.cookies = cookies;
}
@Override
protected final Void doInBackground(Void... voids) {
try {
//And use in your Jsoup
Connection.Response response = Jsoup.connect(YOUR_URL).cookies(cookies).execute();
Document html = response.parse();
...
}
}
Every time you use Jsoup, use the cookies already generated from callback.
This project use this class from zhkrb.
CloudflareScrape is released under the Apache 2.0 license. See LICENSE for details.