Jsoup basic authentication example shows how to access a website with basic authentication using Jsoup. The example also shows how to send the authorization header with Jsoup connection.
How to access websites having basic authentication using Jsoup?
HTTP basic authentication uses a standard header field to authenticate the client request. The client needs to send the “Authorization” header containing the username and password to access the resource. Below given is the format of the “Authorization” header.
a) A user id and password string is created like “username:password” (separated by a colon)
b) A user id and password string in the above format is encoded in base64 encoding.
c) Method of authorization followed by space is put before the encoded user id and password string. So, the final “Authorization” header field’s value looks like,
1 |
"Basic encoded_userid:password" |
You can use the Apache commons codec to encode user id, password string to base64 encoding. You can download the codec from http://commons.apache.org/proper/commons-codec/. Add the library to the classpath so that we can refer to it.
Once you have encoded the user id and password string, you can set the “Authorization” header in above given format and connect to the webpage which requires HTTP basic authentication using Jsoup as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.javacodeexamples.libraries.jsoup; import java.io.IOException; import org.apache.commons.codec.binary.Base64; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsoupBasicAuthenticationExample { public static void main(String[] args) { //replace these values with your own String strURL = "http://localhost/BasicAuthApp"; String strUserId = "admin"; String strPasword = "admin_pass"; /* * User id, password string needs to be in * userid:password format with no space * in between them */ String authString = strUserId + ":" + strPasword; //encode the authString using base64 String encodedString = new String( Base64.encodeBase64(authString.getBytes()) ); /* * connect to the website using Jsoup * and provide above value in Authorization header */ try{ Document document = Jsoup.connect(strURL) .header("Authorization", "Basic " + encodedString) .get(); System.out.println("Logged in using basic authentication"); }catch (IOException e){ e.printStackTrace(); } } } |
Output
1 |
Logged in using basic authentication |
If the above-given code is not working for you, please double-check the user id and password. Also, try to open the page in the browser and logging using the user id and password to make sure that they are valid.
This example is a part of the Jsoup tutorial with examples.
Please let me know your views in the comments section below.
I tried the above code to connect to webpage of a router. However it returns the following error “org.jsoup.HttpStatusException: HTTP error fetching URL. Status=401, URL=http://192.168.xx.xx”
HTTP 401 is an authentication error. It usually means that the page you are trying to access needs some kind of authentication credentials (login id/password) before you can access it. If you have followed the above steps, the reason could be either
1) The server is expecting the authentication in method other than BASIC authentication (e.g. DIGEST) or
2) Credentials you have provided are wrong or not encoded as expected.
In some cases, server may also send HTTP 401 if it is configured to reject requests from unknown user agents. Try to change the user agent if the above given steps don’t apply.