Jsoup and 403 error

In a project I was loading the title of a web page with the following call using the Jsoup library:

String htmlTitle = Jsoup
        .connect(url)
        .timeout(5000)
        .get()
        .title();

This was returning an error 403 on some webpages. I could fix that by passing along an useragent string:

String htmlTitle = Jsoup
        .connect(url)
        .timeout(5000)
        .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36")
        .get()
        .title();

I just took the actual value from my Chrom browser. It seems some sites are picky about clients not identifying themselves.

Leave a Reply

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