Delight-im高级网络视图通过每个GET&发送自定义标头POST请求 [英] Delight-im advanced webview send custom headers with every GET & POST request
问题描述
我正在使用delight-im/Android-AdvancedWebView加载带有附加标头(用户身份验证令牌)的网址,但标头仅在初始请求时发送,即该Webview所加载的第一个网址.但是,当我单击该URL中的另一个链接并尝试发布一些值时,标题未传递.如何将其扩展为具有与基本url相关联的url的所有GET和POST请求? 请提及一种执行此操作的方法. 这是我的代码示例,用于通过webview加载网址.
I am using delight-im/Android-AdvancedWebView for loading a url with extra headers (user auth token), but the headers are sending only with the initial request, that is the first url the webview is loading. But when I click another link in that url and try to POST some value the header is not passed along. How it can be extended to all GET and POST requests with the url's associated with base url ? Please mention a way to do this. Here is my code sample for loading the url with webview.
public class TableViewTest extends AppCompatActivity implements AdvancedWebView.Listener {
SharedPreferences pref;
boolean preventCaching = true;
private static final String URL = "my-url";
private AdvancedWebView mWebView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view_test);
pref = getSharedPreferences("LoginActivity", Context.MODE_PRIVATE);
final String acToken = pref.getString("token", "DEFAULT");
//used this method to add headers with every request, not working
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + acToken);
//mWebView.loadUrl(your url, headers);
//webView = (WebView) findViewById(R.id.webView1Id);
mWebView = (AdvancedWebView) findViewById(R.id.webview);
mWebView.setListener(this, this);
mWebView.setGeolocationEnabled(false);
mWebView.setMixedContentAllowed(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//mWebView.setCookiesEnabled(true);
////////////////////////////////////////////////////////////////////////////////////////////
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.clearCache(true);
////////////////////////////////////////////////////////////////////////////////
//mWebView.setThirdPartyCookiesEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Toast.makeText(TableViewTest.this, "Finished loading", Toast.LENGTH_SHORT).show();
/////////////////////////////////////////////////////////////////////////////////////////////
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.clearCache(true);
////////////////////////////////////////////////////////////////////////
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
Toast.makeText(TableViewTest.this, title, Toast.LENGTH_SHORT).show();
}
});
//default method for adding header for initial method in AdvancedWebview
mWebView.addHttpHeader("Authorization", "Bearer " + acToken);
mWebView.loadUrl(URL, headers);
// webView.getSettings().setJavaScriptEnabled(true);
// webView.getSettings().setBuiltInZoomControls(true);
// webView.getSettings().setDomStorageEnabled(true);
// webView.getSettings().setUseWideViewPort(true);
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
// ...
}
@SuppressLint("NewApi")
@Override
protected void onPause() {
mWebView.onPause();
// ...
super.onPause();
}
@Override
protected void onDestroy() {
mWebView.onDestroy();
// ...
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
@Override
public void onBackPressed() {
if (!mWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
mWebView.setVisibility(View.INVISIBLE);
}
@Override
public void onPageFinished(String url) {
mWebView.setVisibility(View.VISIBLE);
mWebView.clearCache(true);
mWebView.getSettings().setAppCacheEnabled(false);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Toast.makeText(TableViewTest.this, "onPageError(errorCode = "+errorCode+", description = "+description+", failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
Toast.makeText(TableViewTest.this, "onDownloadRequested(url = "+url+", suggestedFilename = "+suggestedFilename+", mimeType = "+mimeType+", contentLength = "+contentLength+", contentDisposition = "+contentDisposition+", userAgent = "+userAgent+")", Toast.LENGTH_LONG).show();
/*if (AdvancedWebView.handleDownload(this, url, suggestedFilename)) {
// download successfully handled
}
else {
// download couldn't be handled because user has disabled download manager app on the device
}*/
}
@Override
public void onExternalPageRequest(String url) {
Toast.makeText(TableViewTest.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
}
推荐答案
您可以添加以下代码:
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getExtraHeaders());
return true;
}
});
其中getExtraHeaders()
返回一个Map<String, String>
,其中包含要在此URL的HTTP请求中使用的其他标头.
where getExtraHeaders()
returns a Map<String, String>
containing the additional headers to be used in the HTTP request for this URL.
另外,请注意,addHttpHeader
现在已得到修复,并随每个GET请求发送自定义标头.
Also, please note that addHttpHeader
has been fixed now and sends custom headers with every GET request.
这篇关于Delight-im高级网络视图通过每个GET&发送自定义标头POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!