Is there a way in Jsoup to load a document from a website with basic access authentication?
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors. jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do.
jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.
With HTTP basic access authentication you need to send the Authorization
header along with a value of "Basic " + base64encode("username:password")
.
E.g.
String username = "foo";
String password = "bar";
String login = username + ":" + password;
String base64login = Base64.getEncoder().encodeToString(login.getBytes());
Document document = Jsoup
.connect("http://example.com")
.header("Authorization", "Basic " + base64login)
.get();
// ...
(explicit specification of character encoding in getBytes()
is omitted for brevity as login name and pass is often plain US-ASCII
anyway; besides, Base64 always generates US-ASCII
bytes)
//Log in
Response res = Jsoup
.connect("url")
.data("loginField", "login")
.data("passwordField", "password")
.method(Method.POST)
.execute();
Document doc = res.parse();
//Keep logged in
Map<String, String> cookies = res.cookies();
Document doc2 = Jsoup
.connect("url")
.cookies(cookies)
.get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With