gpt4 book ai didi

java - Android中拦截WebView请求

转载 作者:行者123 更新时间:2023-12-01 17:44:57 32 4
gpt4 key购买 nike

我喜欢在 Android 的请求 header 中添加授权。我尝试过下面的代码但没有成功。后面的 .NET MVC header 为 null。我已经在谷歌上搜索这个问题一天了。请帮助或告诉我我做错了什么

       webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setDomStorageEnabled(true);

webView.loadUrl("http://10.0.2.2:4200", getCustomHeaders());

webView.setWebViewClient(new WebViewClient(){
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
request.getRequestHeaders().put("ClientId", "ANDROID");
request.getRequestHeaders().put("Authorization", "Bearer Vd7NPdgQYU54DgX769T55M");
return super.shouldInterceptRequest(view, request);
}
});

private Map<String, String> getCustomHeaders()
{
Map<String, String> headers = new HashMap<>();
headers.put("ClientId", "ANDROID");
headers.put("Authorization", "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.");
return headers;
}

最佳答案

您必须创建“WebViewClient”类并设置它。Int WebViewClient,您可以重写函数如下。

@Override
public boolean shouldInterceptRequest(WebView view, WebResourceRequest request) {

Map<String, String> headers = new HashMap<>();
headers.put("ClientId", "ANDROID");
headers.put("Authorization", "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.");
view.loadUrl(url, headers );

return new WebResourceResponse("", "", null);
}

关于java - Android中拦截WebView请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60878498/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com