Skip to content

Jsoup basic authentication example

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,

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.

Output

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.

About the author

2 comments

  1. 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”

    1. 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.

Leave a Reply

Your email address will not be published.