从 url android 获取最终重定向的 url

Posted

技术标签:

【中文标题】从 url android 获取最终重定向的 url【英文标题】:Get the final redirected url from url android 【发布时间】:2021-09-15 02:54:14 【问题描述】:

假设我有一个 url,我尝试获取重定向的 url。我使用了以下代码:

fun getRedirectUrl(url: String?): String? 
        var urlTmp: URL? = null
        var redUrl: String? = null
        var connection: HttpURLConnection? = null
        try 
            urlTmp = URL(url)
         catch (e1: MalformedURLException) 
            e1.printStackTrace()
        
        try 
            connection = urlTmp!!.openConnection() as HttpURLConnection
         catch (e: IOException) 
            e.printStackTrace()
        
        try 
            connection!!.responseCode
         catch (e: IOException) 
            e.printStackTrace()
        
        redUrl = connection!!.url.toString()
        connection.disconnect()
        return redUrl
    

即使在 java 编译器中似乎工作我在 android 中得到以下错误(从 logcat 复制粘贴)。

 android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
        at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
        at java.net.InetAddress.getAllByName(InetAddress.java:1152)
        at com.android.okhttp.Dns$1.lookup(Dns.java:41)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
        at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)

我也知道这个post。为什么会发生这种情况?有什么想法吗?

【问题讨论】:

【参考方案1】:

问题是我不应该在主线程上运行这样的代码。使用 AsyncTask 解决了这个问题。示例

private class getRedirectUrl(url:String) : AsyncTask<String?, Int?, String>() 
        val url = url
        override fun doInBackground(vararg p0: String?): String 
            var urlTmp: URL? = null
            var redUrl: String? = null
            var connection: HttpURLConnection? = null
            try 
                urlTmp = URL(url)
             catch (e1: MalformedURLException) 
                e1.printStackTrace()
            
            try 
                connection = urlTmp!!.openConnection() as HttpURLConnection
             catch (e: IOException) 
                e.printStackTrace()
            
            try 
                connection!!.responseCode
             catch (e: IOException) 
                e.printStackTrace()
            
            redUrl = connection!!.url.toString()
            connection.disconnect()
            return redUrl
        

        override fun onPostExecute(result: String?) 
            super.onPostExecute(result)
            Log.d("Redirected URL",""+result)
        
    

现在您可以使用

获取重定向的 url
  getRedirectUrl("the url you want to get redirect").execute()

【讨论】:

以上是关于从 url android 获取最终重定向的 url的主要内容,如果未能解决你的问题,请参考以下文章

重定向 curl 后获取最终 URL

从 Android Webview 获取 POST 数据

如何获取网站重定向目标网址(最终用户链接)

使用curl获取Location:重定向后url

[Android ]如何在 Android 应用上获取重定向 url

Android WebViewClient url 重定向(Android URL 加载系统)