How to intercept WebView request and add query parameters? - android

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?

Related

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.

Maintaining a HTML session with a cookie SID JSOUP

I'm working on an android application which logs into a website. Basically I have managed to login within Activity A. The user is then redirected to activity B, I want to maintain that logged in session while in activity B. In order to achieve this I have tried this:
Document postLogin = Jsoup.connect("http://forums.d2jsp.org/")
.cookie("sid", sidDetails.getSid())
.post();
As you can see i'm trying to POST the cookie SID. This obviously isn't working, so does anyone have any ideas on how to achieve this? It should be noted I am using JSOUP.
There's not really enough here to go on. You need to figure out what the site is using to maintain login state, and make sure you're setting up Jsoup to send that data.
What I would suggest is that you use Charles Proxy to trace the requests you make in a regular browser to this site, then make sure you're setting up Jsoup to send the right data. By watching the HTTP traffic over the wire, you'll get a better picture of what's happening, and hopefully spot what's missing.
If you can't see how to get Jsoup to add the right data to the request, just give an example of what you need.

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?

Detect and append POST parameters in Android WebView

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.

How can I prevent an Android WebView from manipulating a web page?

As shown here it is possible to:
1. Create an Android WebView with setJavaScriptEnabled=true
2. Load a URL
3. Override onPageFinished and change the Web page displayed to the user
Now, is it possible for the Web page owner to implement anything server-side which prevents this from happening? The risk is that a malicious app could spoof the user by amending the Web page for nefarious means...
Many thanks!
It is not really possible to prevent the web response being modified. You can only think of making it difficult for the spoofer to edit it.

Categories

Resources