android - Loading a JSoup Document into a webview after auto authentication -


i have been working tirelessly learn , implement auto authentication on website use @ university. believe have set auto authentication part of app, not know how display website them passed login stage. know how before auto authentication since have used jsoup auto authenticate left document variable holding webpage. possible load document webview user continue using website if had manually logged in or going @ problem wrong way? unable use .join() method on thread have not handled error, unsure or how should handling interuptexception.

here code far:

`

import android.graphics.bitmap; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.webkit.cookiemanager; import android.webkit.webview; import android.webkit.webviewclient;  import org.apache.commons.codec.binary.base64; import org.jsoup.jsoup; import org.jsoup.nodes.document; import java.io.ioexception;  public class mainactivity extends appcompatactivity {      private string url;     private webview web;     private document doc;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         web = (webview) findviewbyid(r.id.webview);         cookiemanager.getinstance().setacceptcookie(true);         web.getsettings().setdomstorageenabled(true);          system.out.println("about try!!!!!!!!");          thread thread = new thread(new runnable()         {              @override             public void run()             {                  string username2 = "*********";                 string password2 = "*********";                 string login = username2 + ":" + password2;                 url = "https://hrorganiser.essex.ac.uk/";                  string base64login = new string(base64.encodebase64(login.getbytes()));                  try                 {                     doc = jsoup.connect(url).header("authorization", "basic " + base64login).get();                      system.out.println(">> connection successful");                 }                 catch (exception e)                 {                     e.printstacktrace();                     system.out.println(">> connection "+url+" failed");                 }             }         });          thread.start();         thread.join();         if (doc == null) {             system.out.println("error connecting...");         } else {             web.loadurl(doc.html());         }     } }` 

when authenticated jsoup, response of jsoup contain cookies server. server gives cookie identify , prove authenticated user.

in case, need get cookie jsoup , make webview use cookie.

in order cookie, instead of:

doc = jsoup.connect(url).header("authorization", "basic " + base64login).get(); 

you need do:

connection.response res = jsoup.connect(url).header("authorization", "basic " + base64login).method(method.get).execute(); // might need use "method.post" parameter method 

then get cookie:

map<string, string> cookies = res.cookies(); // cookie document doc =  res.parse(); // html document 

now need set cookiemanager cookie, you'll able use webview logged in user. find out more how make cookiemanager use cookie, please follow great answer here. you'll need re-format cookies string follows standard http request header


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -