1

I am trying to extract some info from a website which requires login to access said page. I created a webview to allow to user to log in first.

The question is, how to use the authorization info from that webview in other HTTP requests?

1 Answers1

0

You can extract the cookies from the page and use them in later requests.

@Override
public void onPageFinished(WebView view, String url){
    String cookies = CookieManager.getInstance().getCookie(url);
    Log.d(TAG, "All the cookies in a string:" + cookies);
}

see more here: Android - extracting cookies after login in webview

Community
  • 1
  • 1
Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
  • And how do I use said cookies? – David Banker May 17 '16 at 18:32
  • depends what you want to do with it. Are you trying to make http calls? or use it in another webview? or.... – Tomer Shemesh May 17 '16 at 18:33
  • How in that cookies you will know if the login process was successful? – hcarrasko May 17 '16 at 18:42
  • Prefereably http calls. – David Banker May 17 '16 at 18:43
  • @Hector well if it goes to a specific url when it logs in or errors you could check that. Most pages have that. Also some pages dont give you a valid cookie until the login is successful. It all depends on the specific website. – Tomer Shemesh May 17 '16 at 18:45
  • Let's assume login is successful, how do I use the cookies in a http request then? – David Banker May 17 '16 at 18:47
  • @DavidBanker When making an http call there will be ways to add headers. just add the header with key Cookie and the value cookies string we have just gotten – Tomer Shemesh May 17 '16 at 18:48
  • @TomerShemesh ah ok, then I should to create a cookie in the web app (ie. called "logsOK") to set if the logs in was successful, then in Android ask if this cookie exist? – hcarrasko May 17 '16 at 18:50
  • i think your getting mixed up. you shouldnt be creating any cookies. The website you are going to in the webview will create a cookie. Every website is different in what the cookie is called. you can use the chrome debug menu to view all the cookies and see whats created when you login. – Tomer Shemesh May 17 '16 at 18:52