Is it possible to intercept complete requests made by webview in android?
I'm looking for something that would function to similar to
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("foo://")) {
// magic
return true;
}
return false;
}
However i would like to be able to get the entire http request and convert it to a string , and do the same for resource requests.
Related
I'm trying to add authorization headers in every request in WebView. I can override shouldOverrideUrlLoading method for GET request, but I can't get POST request to work. I have tried many answer from this site, and none of them works. Is there a proper way to do this?
Edit:
For GET Request I use:
webView.setWebViewClient(new WebViewClient() {
#Override
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString(), getHeader());
return true;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getHeader());
return true;
}
}
I used WebViewClient.shouldInterceptRequest() to intercept POST request. But somehow the request I create became GET.
In my android app I am connecting to a secure site where my login credentials are contained in custom headers. I am able to log in successfully because the custom headers are sent with the new page request.
Based on my custom header information there is specific page functionality which is enabled for my device. The problem is that when I load resources from the home page after login the custom headers that I specify in the webview.LoadUrl(); are not sent. So the end result is that I can log in but do not receive the special functionality that is associated with my device.
I have tried both of these overrides. shouldOverrideUrlLoading seems to work when changing URL's but shouldInterceptRequest does not seem to get called on resource requests? If it is my implementation does not work?
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
request.getRequestHeaders().putAll(getExtraHeaders());
return super.shouldInterceptRequest(view, request);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, getExtraHeaders());
return false;
}
See if this works a little better for you:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.post(new Runnable() {
#Override
public void run() {
view.loadUrl(url, getExtraHeaders());
}
});
// true means: yes, we are overriding the loading of this url
return true;
}
This additional code is just a suggestion/outline and should not be taken as cut/paste ready code
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String mimetype;
String encoding;
Map<String, String> headers = new HashMap<>();
headers.putAll(request.getRequestHeaders());
headers.putAll(getExtraHeaders());
URL url = request.getUrl().toString();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
for (String key : headers.keySet()) {
conn.setRequestProperty(k, headers.get(k));
// TODO look for the mimetype and encoding header information and set mimetype and encoding
}
return new WebResourceResponse(mimetype, encoding, conn.getInputStream());
// return null here if you decide to let the webview load the resource
}
Maybe try a different approach, store whatever your need in a cookie for your host using WebKit's CookieManager and use the request's cookie header instead of your custom headers
The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).
I have a custom WebViewClient, in which I have the following method :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String modifiedUrl = Util.someMethod(url);
super.shouldOverrideUrlLoading(view, modifiedUrl);
}
Will it work or should I put this logic in another method, for example onPagestarted ?
You rather should do something like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(conditionForModifyingUrl){
String modifiedUrl = Util.someMethod(url);
view.loadUrl(modifiedUrl);
return true;
}
return false;
}
Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.
I have some logic I want my WebView to apply whenever I try to change its url. For links within loaded pages it's fine, I can just do it by using shouldOverrideUrlLoading method on my custom WebViewClient.
But when I explicitly call loadUrl on my WebView, shouldOverrideUrlLoading is not fired (obviously, or it would produce an endless loop when calling loadUrl from within this method).
Is there a way to "preload" a url manually so the shouldOverrideUrlLoading method is called?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
This works fine when I click a link from within the rendered page. But if I call:
mWebView.loadUrl("myapp://whatever");
shouldOverrideUrlLoading() is not called. What I am asking is whether there's a way that I can pass a url to my webView so it gets passed through to shouldOVerrideUrlLoading() before actually loading it. (i.e. shouldOVerrideUrlLoading() is called whenever a new url is about to be loaded. That's the event I want to fire)
REF: Android's official WebView documentation
if your app version greater then 'N' you should use this function
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
check you function and version
If all you're trying to do is re-use your logic, just extract it into your WebViewClient and use that to load the url and then just delegate to this in shouldOverrideUrlLoading():
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(view, url);
}
public boolean loadUrl(WebView view, String url) {
if(url.startsWith("myapp://")
{
//Do something
}
else {
view.loadUrl(url);
}
return true;
}
Then instead of
mWebView.loadUrl("myapp://whatever");
You do:
mWebViewClient.loadUrl(mWebView, "myapp://whatever");
I am trying to develop an Android browser application using WebView which enables users to access content from a custom protocol. The custom protocol could be foobar://
I want to intercept all requests to this custom protocol. This means:
GET requests
POST requests
and I need to be able to hand the results of these operations back to the WebView.
The GET requests can be handled using shouldInterceptRequest (available from API level 11).
Now my problem is: How can I incercept and handle POST requests?
Nearly the same question has been asked here and here, however no solutions for their problems have been found.
have you tried overriding for the post method doing something like:
private class ViewerWebViewClient extends WebViewClient {
#Override
public void onPageFinished( WebView view, String url ) {
}
#Override
public boolean shouldOverrideUrlLoading( WebView view, final String url ) {
if(!url.contains(MYKEYWORD))
{
Toast.makeText(getActivity(),POSTING, Toast.LENGTH_LONG).show();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
its just an idea. that maybe could help you.