【问题标题】:Android - Default user agent for URLConnection?Android - URLConnection 的默认用户代理?
【发布时间】:2013-01-05 04:31:58
【问题描述】:

我正在使用此代码创建常规 HTTP 连接:

URLConnection cn = new URL( "http://...." ).openConnection();
cn.connect();

如何找到我的 HTTP 连接的默认用户代理?我尝试使用以下代码,但它们都返回 null:

Log.d("My app", "User agent = " + cn.getRequestProperties().get("User-Agent"));
Log.d("My app", "User agent = " + cn.getHeaderField("User-Agent"));

【问题讨论】:

    标签: android http default user-agent urlconnection


    【解决方案1】:

    默认用户代理为空,因为标头默认为空。您必须手动设置它:

    cn.setRequestProperty("User-Agent","your user agent");
    

    【讨论】:

      【解决方案2】:

      使用默认用户代理:

      URLConnection cn = new URL("http://....").openConnection();
      cn.setRequestProperty("User-agent", System.getProperty("http.agent"));
      cn.connect();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-16
        • 1970-01-01
        • 2017-01-22
        相关资源
        最近更新 更多