0

I have been working tirelessly to learn and implement auto authentication on a website I use at university. I believe that I have now successfully set up the auto authentication part of my app, however I now do not know how to display the website to them now that they are passed the login stage. I know how I would do this before the auto authentication but since I have used JSoup to auto authenticate I am just left with a Document variable holding the webpage. Is it possible to now load that Document into the WebView for the user to continue using the website as if they had just manually logged in or am I going at this problem the wrong way? I am also unable to use the .join() method on my thread as I have not handled an error, but I am unsure where or how I should be handling this InteruptException.

Here is my code so 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 to 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 To "+url+" Failed");
                }
            }
        });

        thread.start();
        thread.join();
        if (doc == null) {
            System.out.println("Error Connecting...");
        } else {
            web.loadUrl(doc.html());
        }
    }
}`
GetSwifty
  • 13
  • 1
  • 5

1 Answers1

0

When you are authenticated with Jsoup, the response of Jsoup will contain the cookies from the server. The server gives you this cookie to identify you and prove that you are an authenticated user.

In your case, you just need to get the cookie with Jsoup and make your WebView use the cookie.

In order to get the cookie, instead of:

doc = Jsoup.connect(url).header("Authorization", "Basic " + base64login).get();

You need to do:

Connection.Response res = Jsoup.connect(url).header("Authorization", "Basic " + base64login).method(Method.GET).execute(); // you might need to use "Method.POST" for the parameter for method

Then get the cookie:

Map<String, String> cookies = res.cookies(); // get the cookie
Document doc =  res.parse(); // get the html document

Now you just need to set your CookieManager with this cookie, then you'll be able to use WebView as a logged in user. To find out more about how to make your CookieManager use this cookie, please follow the great answer here. You'll need to re-format your cookies to a String that follows a standard HTTP request header

Community
  • 1
  • 1
Joel Min
  • 3,387
  • 3
  • 19
  • 38
  • I have tried what you have said to no avail, and all examples online require me to use and implement other libraries. I can not find any information or help on how to add a cookie to CookieManager (which I am assuming is the java.net.CookieManager) using the code you have supplied me with. The 'great answer' also seems to use libraries that I am not, unless I am just being dumb... but I can not create a variable of type Cookie at all with what I have available to me. Also the cookies map seems to be empty using both the POST and GET methods. – GetSwifty Apr 21 '16 at 14:11