-1

I use Jsoup connect to a url and get HTML from this url and I want to detect what user-agent of HTML response. Please teach me if you know!

1 Answers1

1

To find out if the webserver you are connecting to responds with different HTML content depending on the user-agent, I do not see another way than trial and error.

Here is how to set the user-agent in JSoup:

Response response= Jsoup.connect(location)
       .ignoreContentType(true)
       .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36")  
       .referrer("http://www.google.com")   
       .timeout(10000) 
       .followRedirects(true)
       .execute();

Document doc = response.parse();

I added also some other useful modifications ot the request, like setting the referrer, a timeout etc. For detailed information about these methods look into the Jsoup documentation: https://jsoup.org/apidocs/org/jsoup/Connection.html

If you want to try our different user-agent strings, I would recommend looking up what is out there in the wild. A collection can be found here: http://www.useragentstring.com/pages/useragentstring.php

luksch
  • 11,497
  • 6
  • 38
  • 53