Detect and append POST parameters in Android WebView - android

I am developing an app for Android that relies heavily on internet content. In the app I have a WebView that lets the user browse our secure website. This is working great. However some times we need to trigger a method in our native app.
To do this we need to intercept a POST request when the user submits a form, look for a specific parameter, and if the parameter is there, append another parameter to the POST request before submitting it.
It is important that our app supports older APIs. At least API level 8. In iOS this was a piece of cake. We just intercepted the URL reques and appended our url encoded parameter. In Android however it is much harder. I am unable to find a way to intercept the request without making any changes to our website.
I have tried overriding the webviews loadUrl() method and postUrl() method, but it seems they are not called when the user interacts with a href in the webview. The specific parameter we are looking for can come from different urls, so it is not enought to override the url itself or hardcode the form name. I hope someone is able to help me with this problem.

You can use javascript to intercept the posting. As you can validate all inputs with javascript you can also set up all key=value pairs. The javascript itself can only execute a get request. If that is not allowed use an android javascript bridge to execute a post.

Check out the WebViewClient http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient(android.webkit.WebViewClient) for the webview.
Using this you can handle all the request going through your webview.

Related

How to intercept WebView request and add query parameters?

I have an Android app that uses a WebView to view a website. For certain URLs, I want to adjust what the WebView loads. For example, if the WebView tries to go to:
https://www.example.com/abc
I want to intercept that request before it goes to server, and add some query parameters. For example:
https://www.example.com/abc?a=123&b=yabba
I found this method shouldInterceptRequest, which sounds promising, but if you look at the return value, it appears you need to return the raw data. I don't see a way to modify the request, and then let the WebView proceed with it's request to the HTTP server.
There is also onLoadResource, which is getting called at the right time, just before the WebView sends a request to the server. However, the docs don't mention anything about how to change the request.
Is there a way to do this?

GroupMe API Callback

So, I am trying to use GroupMe's API. The issue is that I don't really know how to get the users access_token once I send them to the site to login. I don't really know how to create a callback or how to use it. So to sum it up
I need to send users to this site
https://oauth.groupme.com/oauth/authorize?client_id=CLIENT_ID,
then they login and groupme sends them to here
https://YOUR_CALLBACK_URL/?access_token=ACCESS_TOKEN.
But I don't know how/where to create a callback url. Then I don't know how to send that access_token back to the app.
Thanks.
A callback URL is simply a url exposed by your app that groupme can redirect users to. The page can be anything, however, oftentimes it takes the user back to your app if it is a webapp, or tells the user to close the page.
The important part is that the url is one that the app controls, so that it can get the contents of the url that contain the token and other data.
If you are writing a web app, then the framework or language should a method or variable you can call or read to get the url. If you are writing a desktop/moble app, one way of creating a callback url is to listen on a tcp port and speak http to the browser. Another way is using a lightweight web server library, or use a lightweight external server like lighthttp and communicate using cgi/fastcgi. All that matters is that you can get the url that groupme se,t the user to.
If you need anymore help, you are using Oauth2 so search for help with that. Nothing that you asked about here is specfic to groupme, so you should be able to use any OAuth2 library.

What are the benefits for Android to add X-Requested-With on WebView requests?

All in the title.
Are there any benefit for anyone that this header is being set every time automatically? Do anyone know the idea behind this spec?
The purpose of having this header automatically added by WebView is to identify requests coming from apps. Apps can use WebView to manipulate organic traffic flow in order to monetize their ads, commit clickfraud, etc. The value of the user agent string can be changed by the app, while the value of this header can't.
Besides that, the header is used by people who need to know who displays their content by analyzing server logs. User agent string can also be used for that, but alone it is often not enough for distinguishing between Chrome (and various browsers cloned from it) and Android WebView (and as I mentioned above apps can change the UA string).
Some people (mostly ad platforms) are also interested knowing what app uses WebView for displaying their content -- X-Requested-With provides exactly that information.
One caveat with X-Requested-With is that jQuery uses it for XHR requests (X-Requested-With: XMLHttpRequest), and some servers don't check the value of this header and always return a JSONed content when they encounter it. Thus, when you try retrieving content from such a server using Android WebView, you receive some JSON garbage instead of expected HTML.

Retrieve the Webview's request's headers

i am working on an android web client that uses a WebView to display webpages.
What i am trying to achieve is this.
When i call webView.loadDataWithBaseURL i want to somehow get the headers that the Webview generates and uses. right now i could not find a webView.getHeaders or anything from a webViewClient which acts as a callback to a webView.
This implementation exists in iOS https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/URLOverview.html#//apple_ref/doc/uid/20001834-BAJEAIEE and i am wondering if it is possible in android/java?
When a user navigates to some authentication login webpage, i need to get the Headers.
any suggestions?

Get url from android browser

is it possible to get callback whenever a url is opened from the android browser.
getallvisitedurls() method returns just url, I want it in realtime and with timestamp of the access.
This is not possible. The Android Browser does not emit a broadcast or anything like that whenever the user opens a URL in it. Besides, the user may be using a third party browser instead of the default one.
The only way you could do this is by making your own browser app and have the user use that one instead of the default browser that came with their ROM.
I believe it is possible to do so by using retrieveRequestToken.
The Twitter API returns a callback for OAuth, so I think it would be possible.
Have a look at this post. It explains how it is to be done and it sounds all good to me.
Note : I haven't tried it myself. So no guarantees. :)

Categories

Resources