示例如下:
/view/webview/WebViewDemo5.java
/**
- WebView 通过 post 加载 url,自定义请求 header,获取或设置 cookie
-
postUrl() - 通过 post 方式加载指定的 url
-
loadUrl() - 追加自定义 header 并加载指定的 url
-
CookieManager - 用于管理 cookie 的类
- 注:
- 1、如果以上方法均不满足需求的话,可以通过 shouldInterceptRequest() 拦截 url 然后通过 URLConnection 等类以自定义的方式请求网络并返回数据
- 2、关于 shouldInterceptRequest() 的基础请参见 view/webview/WebViewDemo5.java
*/
package com.webabcd.androiddemo.view.webview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.webabcd.androiddemo.R;
import com.webabcd.androiddemo.utils.Helper;
public class WebViewDemo5 extends AppCompatActivity {
private TextView mTextView1;
private WebView mWebView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_webview_webviewdemo5);
mWebView1 = findViewById(R.id.webView1);
mTextView1 = findViewById(R.id.textView1);
sample();
}
private void sample() {
// 启用 javascript 支持
WebSettings webSettings = mWebView1.getSettings();
webSettings.setJavaScriptEnabled(true);
// postUrl() - 通过 post 方式加载指定的 url
final String url = "https://www.baidu.com";
String postData = "p1=p1&p2=p2";
mWebView1.postUrl(url, Helper.stringToBytes(postData));
// loadUrl() - 追加自定义 header 并加载指定的 url
// Map<String, String> headers = new HashMap<String, String>();
// headers.put("custom-header", "test");
// mWebView1.loadUrl(url, headers);
// 设置 cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, "mycookie=webabcd");
// 清除全部 cookie
// cookieManager.removeAllCookies(null);
// 持久化设置好的 cookie
cookieManager.flush();
// 获取 cookie
mWebView1.setWebViewClient(new WebViewClient() {
// 本例是在页面加载完成后获取 cookie 的,正常来说服务端是通过 response header 设置 cookie 的,所以拿到 response header 后就能拿到最新的 cookie 了
@Override
public void onPageFinished(WebView view, String url) {
CookieManager cookieManager = CookieManager.getInstance();
String cookieString = cookieManager.getCookie(url);
mTextView1.setText(cookieString);
}
});
}
// 释放资源
@Override
protected void onDestroy() {
// 从父容器中移除 WebView
((ViewGroup) mWebView1.getParent()).removeView(mWebView1);
// 移除 WebView 内的所有控件
mWebView1.removeAllViews();
// destroy() - 销毁
mWebView1.destroy();
mWebView1 = null;
super.onDestroy();
}
}
/layout/activity_view_webview_webviewdemo5.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
pythonpythoneyJ1cGRhdGUiOiB0cnVlLCAidmVyc2lvbiI6IDUuMSwgInVwY29udGVudCI6ICJcdTRmMThcdTUzMTZcdTU2ZGVcdTViYjZcdTU3MzBcdTU3NDBcdTgzYjdcdTUzZDZcdTU0OGNcdTcwZWRcdTk1ZThcdTYzYThcdTgzNTBcdTUxODVcdTViYjlcdWZmMGNcdTU4OWVcdTUyYTBXaW5kb3dzL01hYy9pUGhvbmUvXHU2ZDRmXHU4OWM4XHU1NjY4XHU2M2QyXHU0ZWY2L1x1NmNiOVx1NzMzNFx1ODExYVx1NjcyY1x1N2I0OVx1MzAwMlx1NTM0N1x1N2VhN1x1NjcwOVx1OTVlZVx1OTg5OFx1OGJmN1x1NTJhMFFRL1x1NWZhZVx1NGZlMVx1ZmYxYTM1OTMyMTE1NDIiLCAidXB1cmwiOiAiaHR0cHM6Ly93d2x1LmxhbnpvdW0uY29tL2lZbGw5MHc0dnZwZSIsICJzaG93bWVzc2FnZSI6IGZhbHNlLCAibWVzc2FnZSI6ICJcdThmZDlcdTkxY2NcdTY2MmZtZXNzYWdlNCIsICJtZXNzYWdlX3VybCI6ICIiLCAiaW50ZXJ2YWwiOiAyMCwgIm1vcmVfdXJscyI6ICJodHRwczovLzEwMjRzaGVuLmNvbS9nb2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zOi8venp0dDU3LmNvbS8iLCAiaGVpbGlhb19oZWFkZXIiOiAiIiwgImhlaWxpYW9fZm9vdGVyIjogIiIsICJoZWlsaWFvX2FydGljYWwiOiAiIiwgIm1hemlub3RlIjogIiIsICJzZWh1YXRhbmcxIjogImh0dHBzOi8vd3d3LmhnaGc1OC5jb20iLCAic2VodWF0YW5nMiI6ICJodHRwczovL3d3dy54dmhpNi5jb20iLCAic2VodWF0YW5nMyI6ICJodHRwczovL3d3dy54dmhpNi5jbiIsICJqYXZidXMxIjogImh0dHBzOi8vd3d3LnNlZWphdi5wdyIsICJqYXZidXMyIjogImh0dHBzOi8vd3d3LmJ1c2phdi5mdW4iLCAiamF2YnVzMyI6ICJodHRwczovL3d3dy5qYXZzZWUuY2x1YiIsICJsdW50YW4yMDQ4MSI6ICJodHRwczovLzRzLmFhYTU2Ny5jb20vMjA0OC8iLCAibHVudGFuMjA0ODIiOiAiaHR0cHM6Ly8zcS5nb3V4aWU4LmNvbS8yMDQ4LyIsICJsdW50YW4yMDQ4MyI6ICJodHRwczovL2xzcC5zb3VhaXFpbi5jb20vMjA0OC8ifQ==pythonpython
![](https://img2022.cnblogs.com/blog/2506425/202204/2506425-20220403173022189-492682951.png)