15

我需要将数据发布到服务器(带有“referer”标头字段)并在 Webview 中加载响应。

现在,有不同的方法(来自Android WebView)来做它的一部分,比如:

void loadUrl(String url, Map<String, String> additionalHttpHeaders)

使用指定的附加 HTTP 标头加载给定的 URL。

void loadData(String data, String mimeType, String encoding)

使用“数据”方案 URL 将给定数据加载到此 WebView。

void postUrl(String url, byte[] postData)

使用“POST”方法将带有 postData 的 URL 加载到此 WebView 中。

loadUrl() 允许发送 HttpHeaders 但不允许发送 post 数据,其他方法似乎不允许发送 HttpHeaders。我错过了什么或者我正在尝试的东西是不可能的?

4

2 回答 2

1

You can execute the HttpPost manually like this:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/postreceiver");

// generating your data (AKA parameters)
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ParameterName", "ParameterValue"));
// ...

// adding your headers
httppost.setHeader("HeaderName", "HeaderValue");
// ...

// adding your data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

Get the response as String:

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
    builder.append(line).append("\n");
}
String html = builder.toString();

Now you can put the html into yourWebView by using loadData():

yourWebView.loadData(html ,"text/html", "UTF-8");
于 2014-02-16T12:30:00.887 回答
0

您可以使用继承自 WebView 的自定义类,或者如果您愿意,可以添加扩展功能。逻辑基本相同:

private fun WebView.postUrl(postUrl: String, postData: ByteArray, additionalHttpHeaders: MutableMap<String, String>) {

    val savedWebViewClient = getWebViewClient()

    webViewClient = object : WebViewClient() {
        override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? {

            if (url != postUrl) {
                view.post {
                    webViewClient = savedWebViewClient
                }
                return savedWebViewClient?.shouldInterceptRequest(view, url)
            }

            Log.d("WebView extension", "post ${postData.decodeToString()} to ${url}")
            val httpsUrl = URL(url)
            val conn: HttpsURLConnection = httpsUrl.openConnection() as HttpsURLConnection
            conn.requestMethod = "POST"
            additionalHttpHeaders.forEach { header ->
                conn.addRequestProperty(header.key, header.value)
            }

            conn.outputStream.write(postData)
            conn.outputStream.close()

            val responseCode = conn.responseCode
            Log.d("WebView extension", "responseCode = ${responseCode} ${conn.contentType}")
            view.post {
                webViewClient = savedWebViewClient
            }

            // typical conn.contentType is "text/html; charset=UTF-8"
            return WebResourceResponse(conn.contentType.substringBefore(";"), "utf-8", conn.inputStream)
        }
    }

    loadUrl(postUrl, additionalHttpHeaders)
}

为简洁起见,对上面的代码进行了编辑,隐藏了大多数错误检查。要为 26 级以下的 API 工作,我使用反射来提取savedWebViewClient.

在现实生活中,您还希望覆盖新方法,并将WebViewClientshouldInterceptRequest(view: WebView, request: WebResourceRequest)的所有其他方法委托给. 可能PostWithHeadersWebView类(也覆盖)可以让您的生活更轻松。savedWebViewClientsetWibViewClient()

于 2021-02-24T19:33:03.747 回答