【问题标题】:How to set an user agent and connection timeout for jsoup in android如何在android中为jsoup设置用户代理和连接超时
【发布时间】:2014-08-22 21:51:30
【问题描述】:

我试图在 android 中从 mobile.twitter.com 中提取一些推文,结果我得到了一个混合的 html 文档。经过一番搜索,我意识到我需要设置一个用户代理。

我的目标是设置一个默认用户代理,它不仅适用于我,也适用于使用我的应用程序的其他用户。

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

【问题讨论】:

    标签: java android html-parsing jsoup user-agent


    【解决方案1】:

    如果您使用的是 Android 2.1 或更高版本,那么对于那些可能感兴趣的人来说,有一种获取默认用户代理的简单方法。

    有一个名为http.agent的系统属性,可用于检索User-Agent字符串。

    然后我的代码会变成:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多