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

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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

2 Comments

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

Your email address will not be published. Required fields are marked *

Online Shopping