Reputation: 2887
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.
Upvotes: 0
Views: 687
Reputation: 98
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.
Upvotes: 1