20

Is there a way in Jsoup to load a document from a website with basic access authentication?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user982940
  • 203
  • 1
  • 2
  • 4

2 Answers2

42

With HTTP basic access authentication you need to send the Authorization header along with a value of "Basic " + base64encode("username:password").

E.g. (with little help of Apache Commons Codec Base64):

String username = "foo";
String password = "bar";
String login = username + ":" + password;
String base64login = new String(Base64.encodeBase64(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)

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
6
//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();