Is there a way in Jsoup to load a document from a website with basic access authentication?
            Asked
            
        
        
            Active
            
        
            Viewed 2.9k times
        
    2 Answers
45
            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)
 
    
    
        BalusC
        
- 1,082,665
- 372
- 3,610
- 3,555
5
            
            
        //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();
 
    
    
        Igor Brusamolin Lobo Santos
        
- 1,165
- 11
- 20
- 
                    1The OP asked about http basic authentication. This solution is for form-based auth which is something else entirely. – LinuxDisciple Jan 19 '22 at 22:46
- 
                    It's a useful answer in general. Maybe editing the question to ask about auth in general would lead to a better post? – Yuri Schimke Jan 20 '22 at 09:21
- 
                    @YuriSchimke 1) Editing the question after it has been answered turns existing answers into nonsense because they refer to a different question. 2) The question that corresponds to this answer has been asked plenty of times already, and has better answers than this. 3) I found this question because I searched for specific terms that matched the question I had. If this question were changed, it would no longer match the question I had, and I wouldn't have found this question or BalusC's correct answer. Changing a properly-answered question is never a good idea. – LinuxDisciple Feb 01 '22 at 19:06
