前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Load URL With Extra HTTP Headers Using WebView in Android

Load URL With Extra HTTP Headers Using WebView in Android

作者头像
技术小黑屋
发布2018-09-04 16:40:08
1.1K0
发布2018-09-04 16:40:08
举报
文章被收录于专栏:技术小黑屋

As we know, Webview will add the referer into the reqeust if we click a hyperlink to navigate to another one. But if we want to add a referer when a hard loading starts, What can we do to make it?

Since Froyo(API Level 8), the webview starts providing an interface for us to send extra HTTP headers when loading a URL.

public void loadUrl (String url, Map<String, String> additionalHttpHeaders) Added in API level 8 Loads the given URL with the specified additional HTTP headers. Parameters url the URL of the resource to load additionalHttpHeaders the additional headers to be used in the HTTP request for this URL, specified as a map from name to value. Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overriden by this WebView’s defaults.

Now this is a working example.

1 2 3 4 5 6 7

public void testLoadURLWithHTTPHeaders() { final String url = "http://androidyue.github.io/"; WebView webView = new WebView(getActivity()); Map<String,String> extraHeaders = new HashMap<String, String>(); extraHeaders.put("Referer", "http://www.google.com"); webView.loadUrl(url, extraHeaders); }

For more details about HTTP Headers, please visit List_of_HTTP_header_fields More details about Webview, please visit http://developer.android.com/reference/android/webkit/WebView.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档