java Android WebView 自定义标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32125878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android WebView custom headers
提问by Enve
I am currently using this code to add a custom header to android WebView
我目前正在使用此代码向 android 添加自定义标头 WebView
Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("example", "header");
webView.loadUrl(url, extraHeader);
Above code is working but only on the main page. So if I write this code echo $_SERVER['example']
it prints header
. But there is an iframe
in the loaded URL which shows an undefined error when I try the same code. Is there any way I can fix this?
上面的代码有效,但只在主页面上。因此,如果我编写此代码,echo $_SERVER['example']
它会打印header
. 但是iframe
当我尝试相同的代码时,加载的 URL 中有一个显示未定义的错误。有什么办法可以解决这个问题吗?
So what I want to do is add custom header not only to the main loaded URL but also on the iframe
of the loaded page.
所以我想要做的是不仅向主要加载的 URL 添加自定义标题,还要在iframe
加载的页面上添加自定义标题。
采纳答案by Mikhail Naganov
No, that is not possible with Android WebView itself. You have to work around either in your page code, or in your app's code, or on the server.
不,这对于 Android WebView 本身是不可能的。您必须在页面代码、应用程序代码或服务器上解决。
For fixing this on the page's side, you can use XMLHttpRequest
for loading subresources. But for that you will have basically to construct the page on the fly.
为了在页面一侧修复此问题,您可以使用XMLHttpRequest
加载子资源。但是为此,您基本上必须即时构建页面。
On the app's side, you can use WebViewClient.shouldInterceptRequest
, to intercept all the network requests. You are not allowed to just modify the provided request, instead, you will need to make a new request yourself, but there you will be able to set any headers you want. See this example: Android WebViewClient url redirection (Android URL loading system)
在应用端,您可以使用WebViewClient.shouldInterceptRequest
, 拦截所有网络请求。您不能只修改提供的请求,相反,您需要自己提出一个新请求,但您可以在那里设置您想要的任何标头。看这个例子:Android WebViewClient url redirection(Android URL加载系统)
On the server side, you can look into Referer
header of subresources, which must contain the url of the page that has requested it.
在服务器端,您可以查看Referer
子资源的标题,其中必须包含请求它的页面的 url。
回答by angryd
This worked for me:
这对我有用:
mWebView.setWebViewClient(new MyWebViewClient(token));
in MyWebViewClient
在 MyWebViewClient 中
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
try {
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url.trim())
.addHeader("token", mToken) //add headers
.build();
Response response = httpClient.newCall(request).execute();
return new WebResourceResponse(
getMimeType(url), // set content-type
response.header("content-encoding", "utf-8"),
response.body().byteStream()
);
} catch (IOException e) {
return null;
}
}
//get mime type by url
public String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
if (extension.equals("js")) {
return "text/javascript";
}
else if (extension.equals("woff")) {
return "application/font-woff";
}
else if (extension.equals("woff2")) {
return "application/font-woff2";
}
else if (extension.equals("ttf")) {
return "application/x-font-ttf";
}
else if (extension.equals("eot")) {
return "application/vnd.ms-fontobject";
}
else if (extension.equals("svg")) {
return "image/svg+xml";
}
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
回答by Rahmat Rasyidi Hakim
You can put this setting to your web setting, every request from webview will be using this User-Agent
header.
您可以将此设置放入您的网络设置中,来自 webview 的每个请求都将使用此User-Agent
标头。
webview.getSettings().setUserAgentString("user-agent-string");
webview.getSettings().setUserAgentString("user-agent-string");
回答by Navidonline
Just add this piece of code before load URL:
只需在加载 URL 之前添加这段代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
}