简体   繁体   中英

How to set an user agent and connection timeout for jsoup in android

I was trying to extract some tweets from mobile.twitter.com in an android, and as a result I get a mixed html document. After some searches I realized that I need to set an user agent.

My aim is to set a default user agent which would work not only for me, but also for other users who would use my application.

Document doc = Jsoup.connect("https://mobile.twitter.com/").userAgent(...).get();

For those who might be interested there is an easy way for getting default user agent if you are on Android 2.1 or above.

There is a system property called http.agent , which can be used to retrieve the User-Agent string.

My code would then turn to:

String userAgent = System.getProperty("http.agent");
Document doc = Jsoup.connect("https://mobile.twitter.com/").userAgent(userAgent).get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM