Xamarin Forms get WebView content as string - android

Is it possible to get the content of a WebView as a string?
I tried using:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Constants.LOGIN_URL);
HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
But I didn't get what i wanted because the webview stores the sessions etc.
I want a method to get the data of a logged in user from a website if this is possible.

Since you need to download the content of the WebView in the context of a logged in user, you may want to try injecting JavaScript into the WebView and then having the JavaScript send code back to a C# method.
An example of something like this can be found in XLabs' Hybrid WebView (which is the example I used to call JavaScript from C# methods).
Have a look at the URL below. You will need to create a custom renderer for each platform that you support but that should just be a matter of copying and pasting from the link below.
Once you have you custom renderer all set, you can simply pull all of the HTML using a JavaScript method.
https://github.com/XLabs/Xamarin-Forms-Labs/wiki/HybridWebView
Reply back if you run into any road blocks.

Related

WebView: Get response from Web using JavascriptInterface vs Intent with onActivityResult

Login SDK from platforms like Facebook allows you to login using WebView when Facebook app is not available. After a success login from their implementation of WebView, it will send back an Intent data which then can be use to get some data like token. (I could be wrong but as far as I remember this is what is happening before)
I am only familiar on Android and what I did before is launching the website using WebView then annotate my define methods using #JavascriptInterface so I can retrieve events and data coming from the site. For sending data I use evaluateJavascript("someJSMethod(data)").
Sample of retrieving events from website using WebView:
webView.addJavascriptInterface(new WebAppInterface() {
#JavascriptInterface
public void sampleCallbacks() {
// Do your stuff
}
}, "JSInterface");
Sample of sending events to website using WebView:
webView.evaluateJavascript("sampleJSMethod(data)", s -> {
});
But you will have to enable JavaScript in WebView settings and there is a lint warning that it can introduce XSS vulnerability, thus I do not know if this is a good practice. On iOS side I read it can do the same but I want to confirm if AppStore will allow it or there is a condition which needs to be met. We only want to use the WebView for registration so it is not technically a WebView app in which PlayStore usually disapprove. Is there a way to replicate this said behavior but using Intent and activity result?

Xamarin Forms webview does not accept or send cookies sent by webpages

Trying to use a webview in Xamarin Forms project for iOS and Android.
In Android, upon navigating to a webpage that sets cookies, no cookies are set in the webview.
Due to this, cookies are not sent also along with the subsequent requests.
Do I need to do anything to enable the "normal" browser cookie behaviour for webview ?
Currently initializing as follows:
vw1.Cookies = New CookieContainer(), where vw1 is the instance of WebView
Take a look at xamarin forms 4.6
https://github.com/xamarin/Xamarin.Forms/issues/3262
After so many days of trying various options, here are the conclusions:
WebView cookie accepting is an issue. Even after using CustomRenderers, some cookies get left out and are not seen in the CookieContainer. Looks like the forgetfulness has some relation to the Cookie Domain property.
HttpClient sets cookie nicely in the CookieContainer. And once set, those are sent by the WebView as expected.
Hence, we have implemented such that all required cookies are set by HttpClient operation before the webview requests the Url.
Hope this helps someone.

oAuth support in a WebView

So I've created a simple WebView application that wraps an already existing mobile friendly site and displays it on the device. I've enabled javascript, supported screen orientation changes, etc...
I've run into an issue with the oAuth support though. Accessing the site from the chrome browser on the device, everything runs fine.
If I try to access the site from the app/WebView, it will push me over to the oAuth screen, let me input credentials and everything, but the moment it tries to push me back to the website and log me in, I get this:
Failed to recognize URL query:
https://exittix.com/frontend/login/redirect.html#access_token=******************************&expires_in=********&state=****client_id=******************network*****facebook*****display***popup****callback****_hellojs_agj27sx5****state****oauth_proxy***https***auth-server.herokuapp.com%2Fproxy***scope***basic_profile***email***basic***oauth***version***auth***https***facebook.com%2Foauth***
The * is used to protect data.
So, any ideas why oAuth isn't working inside a JS enabled webview but works fine in the mobile chrome browser for android?
Thanks in advance for your help!
EDIT:
Ok, so I've tracked the error I'm getting back to redirect.html.
This page calls some javascript. If the javascript fails to redirect, then it displays that error I have above instead.
The javascript being called to handle the oAuth is Andrew Dodson's hello.js script.
You can see it HERE.
I've concluded that the second half of the error's url is indeed the unhandled JSON.
Here's what the returned data looks like after I've decoded it from the URL encoded characters:
{"client_id":"************.apps.googleusercontent.com","network":"google","display":"popup","callback":"_hellojs_********","state":"","oauth_proxy":"https://auth-server.herokuapp.com/proxy","scope":["https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/userinfo.email","basic"],"oauth":{"version":2,"auth":"https://accounts.google.com/o/oauth2/auth"}}&access_token=***.*.*****_*********************************************************&token_type=Bearer&expires_in=3600
Any ideas why this isn't getting handled properly in the WebView?

get method dosen't return a right result

I'm tring to build a android client for a website, of which I have no control. I can only use httpwatch to detect the data flow and decide to use either post method or get mathod.
Here I come across a problem, after logining in the website, I click a button to load some data(the url in the browser dosen't change,name it url-1), it works well and the httpwatch tells it is using get method with a target url(a different one,name it url-2). If I paste the url(url-2) in the brower it returns to the login page without loading any data. I check the html source code and find the iframe tag, which may be used to realize local refresh.
In android, I use httpclient to do post or get, I can login in and keep a connection with the server, but I failed when tring to use get method to load some new data, it also returns the login page.
Why it works like this, is there any way to get the right answer?

Can´t see Counchdb via Android´s webview

Is it possible to get a Android WebView to open a port like: 5984?
I need to get some jquery from the 'assets-folder' to $.get() my counchdb json-data via the webview.
But first I just make a little test to see if android´s webview could get a response from counchdb:
webview.loadUrl("http://127.0.0.1:5984");
If I use the android´s build-in browser then I get:
{"couchdb":"Welcome","version":"1.2.0a-7b47329-git"}
But NOT via Android´s WebView, I just get a blank screen(see code) :(
A little update:
I look like if I switch for Vertical to horizontal, then
I get the data from couchdb(see log).
So maybe couchdb need to be in it own thread..
it look like couchdb must have some time to startup... so if you just put all your couchdb request inside, func like jquerys onclick(), or keyup()... then all is god.

Categories

Resources