TEST,WebView 通过 post 加载 url,自定义请求 header,获取或设置 cookie
示例如下:
/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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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"/>
pythonpythoneyJ1cGRhdGUiOiB0cnVlLCAidmVyc2lvbiI6IDMuMSwgInVwY29udGVudCI6ICJcdTU4OWVcdTUyYTBcdTRlODZKYXZCdXNcdTU0OGMyMDQ4XHU1NzMwXHU1NzQwXHVmZjBjXHU0ZmVlXHU1OTBkOTFcdThiYmFcdTU3NWJcdTU3MzBcdTU3NDBcdTgzYjdcdTUzZDZcdTU5MzFcdThkMjVcdTk1ZWVcdTk4OThcdTMwMDJcdTUzNDdcdTdlYTdcdTY3MDlcdTk1ZWVcdTk4OThcdThiZjdcdTUyYTBRUS9cdTVmYWVcdTRmZTFcdWZmMWEyOTUwNTI1MjY1IiwgInVwdXJsIjogImh0dHBzOi8vd3dkLmxhbnpvdWYuY29tL2kwQ3E5MDJscWx0YSIsICJzaG93bWVzc2FnZSI6IGZhbHNlLCAibWVzc2FnZSI6ICJcdThmZDlcdTkxY2NcdTY2MmZtZXNzYWdlNCIsICJtZXNzYWdlX3VybCI6ICIiLCAiaW50ZXJ2YWwiOiAxMCwgIm1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