我正在尝试创建一个简单的 Android 应用程序,其中包含一个 webView,该应用程序加载并打开移动运营商列入白名单的 url,以允许标头丰富注入。我们对移动互联网进行了适当的测试,并且该过程在移动 Chrome 浏览器上顺利进行,但是,当在 Android 应用程序内的 webView 中使用时,注入不起作用。
它仍然是一个简单的 Android 应用程序,包含以下文件:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestHe"
tools:targetApi="31"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt:
package com.testHe.app
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.webkit.CookieManager
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
prepareWebView()
}
private fun prepareWebView() {
val webView = findViewById<WebView>(R.id.webView)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
webView.settings.apply {
javaScriptEnabled = true
javaScriptCanOpenWindowsAutomatically = true
allowFileAccess = true
allowContentAccess = true
allowFileAccessFromFileURLs = true
allowUniversalAccessFromFileURLs = true
mixedContentMode = MIXED_CONTENT_ALWAYS_ALLOW
domStorageEnabled = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
safeBrowsingEnabled = false
}
}
webView.webChromeClient = object: WebChromeClient() {
}
webView.webViewClient = object: WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
//Toast.makeText(this@MainActivity, "page Loading: $url", Toast.LENGTH_SHORT).show()
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
if(request?.requestHeaders != null) {
request.requestHeaders.forEach { action ->
Toast.makeText(this@MainActivity, "header, key: ${action.key}, value: ${action.value}", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this@MainActivity, "headers are null for url: ${request?.url?: ""}", Toast.LENGTH_SHORT).show()
}
return super.shouldOverrideUrlLoading(view, request)
}
}
/*lifecycleScope.launch {
val htmlData = getHtmlData()
webView.loadData(htmlData, "text/html", "utf-8");
}*/
//webView.loadData(htmlData, "text/html", "utf-8");
webView.loadUrl(operatorUrl)
}
private suspend fun getHtmlData(): String = withContext(Dispatchers.IO) {
val google = URL(operatorUrl)
val `in` = BufferedReader(InputStreamReader(google.openStream()))
var input: String?
val stringBuffer = StringBuffer()
while ((`in`.readLine().also { input = it }) != null) {
stringBuffer.append(input)
}
`in`.close()
return@withContext stringBuffer.toString()
}
}
PS:operatorUrl 是实际网络运营商 url 的占位符。在代码中它是实际的 url。 我尝试了 webview 的 loadData 和 loadUrl 方法,但没有成功。
解决方案:重写WebView的User-Agent以匹配设备上的Chrome浏览器。
private fun prepareWebView() {
val webView = findViewById<WebView>(R.id.webView)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
webView.settings.apply {
javaScriptEnabled = true
javaScriptCanOpenWindowsAutomatically = true
allowFileAccess = true
allowContentAccess = true
allowFileAccessFromFileURLs = true
allowUniversalAccessFromFileURLs = true
mixedContentMode = MIXED_CONTENT_ALWAYS_ALLOW
domStorageEnabled = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
safeBrowsingEnabled = false
}
// Set User-Agent to match Chrome
userAgentString = System.getProperty("http.agent")
// Or you can use a specific Chrome User-Agent string
// userAgentString = "Mozilla/5.0 (Linux; Android ${Build.VERSION.RELEASE}; ${Build.MODEL}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Mobile Safari/537.36"
}
webView.webChromeClient = object : WebChromeClient() {}
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
// Handle headers and URL loading here if necessary
return super.shouldOverrideUrlLoading(view, request)
}
}
webView.loadUrl(operatorUrl)
}