I am trying to pull HTML data out of a WebView. I've seen this done a thousand times and I've even gotten it to work myself. However, my new project leads to an interesting situation: I need to login to a site through a WebView and then pull the HTML. Obviously, the Socket method doesn't work, because in order for a webpage to be returned you need the cookie for authentication. I've also tried the JavascriptInterface trick, but that didn't work either. I'm thinking the best way to achieve this is to use HttpGet with CookieManager? Does anyone know how I can get raw HTML code from a WebView with an auth cookie? Thanks!
EDIT: I did some JS injection and didn't see any cookies... so it might not be a cookie issue? But the links that you get redirected to are generic, like mainPage?a=1 and infoPage. You cannot simply copy/paste the links into another browser you have to be logged in to view these links. For those of you who are web experts, you may know an easy solution.
WebView isn't really meant for getting HTML for programmatic use, the idea is that it's just a direct window to a URL for user interaction.
To get what you want, you can use a java.net.HttpURLConnection with a CookieManager, it's worked fine for me on Android and it's suggested in the Android SDK docs:
// in an activity's onCreate:
cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
// later on
void getAPage(URL url) {
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
System.out.println("hello from huc, response code is: " + huc.getResponseCode());
// huc.getInputStream() gives you the content.
}
The CookieManager will persist all cookies through the lifetime of the application without you having to do anything extra. You can make posts with HttpURLConnection too.
Related
I have my app ("myapp") with following example package signature: com.example.mycorp.myapp
I browsed via adb shell the app folder /data/data/com.example.mycorp.myapp/ to find any cookies - in vain.
Since I have WebView objects showing external links (html) and Google Login as an example, there should be some cookies created (at least iOS colleagues have there cookies on the same app for iOS).
So where are can I find any cookies regarding my own app?
Programmatically by means of CookieManager:
Memeber variable
private CookieManager cookieManager = null;
In a onCreate() or another constructor
cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
A checkCookies() Method which can be triggered frequently
List<HttpCokie> cookies = cookieManager.getCookieStore().getCookies();
Now, iterate through cookies and print them out.
If you want to get the cookie value you can use this:
CookieManager.getInstance().getCookie("http://the.url.com")
More help will be provided here:
http://developer.android.com/reference/android/webkit/CookieManager.html#getCookie(java.lang.String)
I'm implementing a payment gateway in my project. Now I have a base url and along with that some encrypted parameters like amount merchant id and all those stuffs.
So when the encryption completes I'm supposed to open it in a webview. If I try to concatenate the strings it doesn't work.
I got this info that while implementing the same for webpage they just use post method and the payment gateway worked. But how to open the same in webview in android device?
I tried this way:
wvSBIBuddy.loadUrl(url);
wvSBIBuddy.loadData(urlCreateOrder.toString(), "application/json", null);
Didn't work. Tried this:
wvSBIBuddy.loadDataWithBaseURL(url,urlCreateOrder,"text/html", null, ENCODING_KEY);
wvSBIBuddy.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
any hint what might work?
EDIT:
Is it possible to add some refer headers along with the encrypted detail?
I looked into the webview class it seems encrypted data and refer both can't be send at the same time, still asking any way to do that?
postUrl takes a byte array, so I don't know if just decoding your string with the default charset into a byte array will work right. But this is how you do a POST:
wvSBIBuddy.postUrl(url, urlCreateOrder.getBytes());
So basically right now my app is configured to use https because in the "release" it will use a self signed certificate and obviously also use Https.
My current testsystem (few more features) doesn't use https but http instead. I thought it would be kinda nice to have some type of method to check whether the given URL is Http or Https and depending on the result create the right URLConnection.
My current problem is that I don't know what the method should exactly look like. I thought about using if-statements in the methods which connect to my server but there might be a better solution.
Help would be appreciated.
How about this:
URLUtil.isHttpUrl(String url)
URLUtil.isHttpsUrl(String url)
See also: http://developer.android.com/reference/android/webkit/URLUtil.html
If you don't want to do a manual check you can use a 3rd party library like this one: http://square.github.io/okhttp/ which allows you a simple:
Request request = new Request.Builder().url(url).build();
Response response = new OkHttpClient().newCall(request).execute();
I am working on a android web application and based on if a secure cookie exists I want to do something. Unfortunately when I use the following line of code I only get a list of unsecure cookies.
String cookies = cookieManager.getCookie(siteName)
Does anyone know how I can get a list of all secure cookies for a specific domain?
You can retrieve secure cookies by using a url that starts with "https://".
String cookies = cookieManager.getCookie("https://example.com");
Returns all cookies (including secure only ones). Logic for getting cookies can be read at: https://github.com/adobe/chromium/blob/master/net/cookies/cookie_monster.cc#L1780 .
To resolve the issue I added the following before I created the webView
CookieManager.setAcceptFileSchemeCookies(true);
see CookieManager.setAcceptFileSchemeCookies(boolean accept)
In my Android app, each activity is filled with data from an xml file which is somewhere on the web. The website providing these files has a login mechanism, that works with cookies.
I know how to make a HTTP Request to the login page and receive a cookie. What I don't know is, how I can store it to re-use it in both other activities AND when the app is started the next time. The cookie is valid for a year, so the user of my app should log in once and then never again for a whole year.
How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.
Best regards and thanks in advance, Jan Oliver
Use a CookieSyncManager to store your cookie value. It can persist across application starts.
Beware of using CookieSyncManager inside of WebViewClient#shouldInterceptRequest on KitKat. It will deadlock.
EDIT
CookieSyncManager was deprecated in API 21:
This class was deprecated in API level 21. The WebView now
automatically syncs cookies as necessary. You no longer need to create
or use the CookieSyncManager. To manually force a sync you can use the
CookieManager method flush() which is a synchronous replacement for
sync().
It looks like Android is using the default in memory implementation so you will need to create your own persistent cookie store.
This Java Tutorial outlines creating your own persistent store taking advantage of the default implementation.
http://download.oracle.com/javase/tutorial/networking/cookies/custom.html
The sample has two todo's for storage (read/write) For storage I would just use SharedPreferences to store just the session cookie that you need and not persist any others.
The sample uses a shutdown hook which is not what you want in Android. In place of run() and the hook I would just have a new public method persist() that saves what you want, though that requires that you persist() the store by hand.
Given that you only have one or two cookies that matter you could save them in the add(...)
After you make your http call, you can grab the cookies like this
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d(TAG,"no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().contentEquals("PHPSESSID")) {
PHPSESSID = cookies.get(i).getValue();
}
}
}
To send them back:
nameValuePairs.add(new BasicNameValuePair("PHPSESSID",phpsessid));
httppost.setEntity(new UrlEncodedFormEntity(aList));
aList is all your nameValuePairs
LoopJ has a built in persistent cookie store that can be used with or without the loopj framework
https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/PersistentCookieStore.java
I wrote a simple class named CookieHelper and I've provided an example of how to use this class to help all users that facing the same problem :
https://github.com/augustopicciani/HttpClient-save-cookies-to-file