【问题标题】:Android - Resolve short urls from intent-filtersAndroid - 从意图过滤器解析短网址
【发布时间】:2015-10-12 13:11:47
【问题描述】:

遇到了一个关于短网址的奇怪问题。

我有一个带有 2 个不同意图过滤器的 Activity 来捕获常规 url,以及同一网站 (Dribbble) 提供的短 url。

这是我的清单:

    <activity
        android:name=".activities.SomeActivity">

        <!-- For "regular" urls -->
        <intent-filter>
            <data
                android:host="dribbble.com"
                android:pathPrefix="/shots/"
                android:scheme="https" />
        ...
        </intent-filter>

        <!-- For short urls -->
        <intent-filter>
            <data
                android:host="drbl.in"
                android:scheme="http" />
        ...
        </intent-filter>
    </activity>

以下是与这些意图过滤器匹配的 2 个 url 示例:

检索正常的网址按预期工作。但是,当尝试解析来自 Android 的股票浏览器的缩短 url 时,Activity 中收到的 url 是两种类型的 url 的混合。即它是由常规url的主机+后缀组成,但是短url的路径。对于前面的示例,它看起来像这样:

所以,当我尝试在我的 Activity 中解析短 url 时,我的代码不会进入条件测试该 url 是否为拍摄 url:

class SomeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        ...
        resolveUrl(getIntent().getData());
        ...
    }

    private void resolveUrl(Uri url) {
        if (url.getHost().equals("drbl.in")) { // Condition never met
            resolveShortUrl(url);
        } else {
            getIdFromUrl(url);
        }
    }

    ... 
}

谁能告诉我我到底做错了什么? 感谢您的帮助。

【问题讨论】:

    标签: java android intentfilter short-url


    【解决方案1】:

    谁能告诉我我到底做错了什么?

    如果你运行 curl -v http://drbl.in/qkHY,你会得到类似于:

    * Hostname was NOT found in DNS cache
    *   Trying 52.6.188.250...
    * Connected to drbl.in (52.6.188.250) port 80 (#0)
    > GET /qkHY HTTP/1.1
    > User-Agent: curl/7.38.0
    > Host: drbl.in
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    * Server nginx/1.4.6 (Ubuntu) is not blacklisted
    < Server: nginx/1.4.6 (Ubuntu)
    < Date: Mon, 12 Oct 2015 13:14:03 GMT
    < Content-Type: text/html
    < Content-Length: 193
    < Location: https://dribbble.com/shots/qkHY
    < 
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.4.6 (Ubuntu)</center>
    </body>
    </html>
    * Connection #0 to host drbl.in left intact
    

    因此,Dribble 将短 URL 重定向到 dribble.com URL,路径中有短代码。 Android 与它无关,除了是执行 HTTP 请求的那个。

    如果您执行 curl -v https://dribbble.com/shots/qkHY(重定向到的 URL),Dribble 会发出 另一个 重定向响应,尽管一个具有 MIME 类型:

    * Hostname was NOT found in DNS cache
    *   Trying 52.6.188.250...
    * Connected to dribbble.com (52.6.188.250) port 443 (#0)
    * successfully set certificate verify locations:
    *   CAfile: none
      CApath: /etc/ssl/certs
    * SSLv3, TLS handshake, Client hello (1):
    * SSLv3, TLS handshake, Server hello (2):
    * SSLv3, TLS handshake, CERT (11):
    * SSLv3, TLS handshake, Server key exchange (12):
    * SSLv3, TLS handshake, Server finished (14):
    * SSLv3, TLS handshake, Client key exchange (16):
    * SSLv3, TLS change cipher, Client hello (1):
    * SSLv3, TLS handshake, Finished (20):
    * SSLv3, TLS change cipher, Client hello (1):
    * SSLv3, TLS handshake, Finished (20):
    * SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
    * Server certificate:
    *    subject: businessCategory=Private Organization; 1.3.6.1.4.1.311.60.2.1.3=US; 1.3.6.1.4.1.311.60.2.1.2=Massachusetts; serialNumber=001031096; street=Ste. 202; street=16 Front St.; postalCode=01970; C=US; ST=Massachusetts; L=Salem; O=Dribbble LLC; CN=dribbble.com
    *    start date: 2014-12-31 00:00:00 GMT
    *    expire date: 2016-04-29 12:00:00 GMT
    *    subjectAltName: dribbble.com matched
    *    issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 Extended Validation Server CA
    *    SSL certificate verify ok.
    > GET /shots/qkHY HTTP/1.1
    > User-Agent: curl/7.38.0
    > Host: dribbble.com
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    * Server nginx/1.4.6 (Ubuntu) is not blacklisted
    < Server: nginx/1.4.6 (Ubuntu)
    < Date: Mon, 12 Oct 2015 13:16:25 GMT
    < Content-Type: text/html; charset=utf-8
    < Transfer-Encoding: chunked
    < Status: 301 Moved Permanently
    < X-Frame-Options: SAMEORIGIN
    < X-XSS-Protection: 1; mode=block
    < X-Content-Type-Options: nosniff
    < Location: https://dribbble.com/shots/2278534-Google-Now-Animation
    < Cache-Control: no-cache
    < Set-Cookie: _dribbble_session=eGhSVTlDV1NEMzBiTUlLMTA0YWlkQ0YzWUJiYUM2cGVGQ1J1cFZYTE9xL3R3UEsvb0VWS1paU1U3bkNRdlh1V3JJZ2JqdW1veTM0azMrTVFRUUlMYUE9PS0tdC9uQTlNR0xvKy9GQmx0R3BkTUtWQT09--9873ba3016e4ab9a99f6a237a975610c01c1eb15; path=/; secure; HttpOnly
    < X-Request-Id: 5359c3d6-9d64-4637-8ff5-014e8e44d503
    < X-Runtime: 0.006814
    < Strict-Transport-Security: max-age=31536000
    < X-Frame-Options: SAMEORIGIN
    < 
    * Connection #0 to host dribbble.com left intact
    <html><body>You are being <a href="https://dribbble.com/shots/2278534-Google-Now-Animation">redirected</a>.</body></html>
    

    HTTP HEAD 请求(将 -X HEAD 添加到 curl 命令)返回相同的基本内容,但不包括正文。

    我最好的猜测是,Android 会在第二个 HEAD 请求中看到 Content-type 标头,然后继续使用该 URL 进行 Intent 解析。您对此无能为力,因为我认为您既不能控制 Android 也不能控制 Dribble。但是,您始终可以通过在 Java 代码中发出自己的 HEAD 请求来自己获取完整的 URL。

    【讨论】:

    • 感谢您提供快速、详细和准确的答复。将尝试使用它。
    • @MathieuMaree 今天仍然如此吗?你是怎么解决这个问题的?
    猜你喜欢
    • 2011-07-27
    • 2015-05-14
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多