Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSoup getting content type then data

so currently I'm retrieving the data from a url using the following code

Document doc = Jsoup.connect(url).get();

Before I fetch the data I've decided I want to get the content type, so I do that using the following.

Connection.Response res = Jsoup.connect(url).timeout(10*1000).execute();
String contentType = res.contentType(); 

Now I'm wondering, is this making 2 separate connections? Is this not efficient? Is there a way for me to get the content type and the document data in 1 single connection?

Thanks

like image 394
Belgin Fish Avatar asked Dec 19 '22 15:12

Belgin Fish


1 Answers

Yes Jsoup.connect(url).get() and Jsoup.connect(url).timeout(10*1000).execute(); are two separate connections. Maybe you are looking for something like

Response resp = Jsoup.connect(url).timeout(10*1000).execute();
String contentType = res.contentType(); 

and later parse body of response as a Document

Document doc = resp.parse();

Anyway Jsoup by default parses only text/*, application/xml, or application/xhtml+xml and if content type is other, like application/pdf it will throw UnsupportedMimeTypeException so you shouldn't be worried about it.

like image 75
Pshemo Avatar answered Jan 08 '23 05:01

Pshemo