0

Currently, I'm doing an application where I need to login to a news website but for some reason I receive a warning and fail to login into the website at the same time.

News website: http://www.malaysiakini.com/

News website html form: News website form

My source code:

 private void login(String username, String password){


    try {

        Connection.Response response = Jsoup.connect("http://www.malaysiakini.com/")
                .data("username", "username", "password", "password", "action","//login2.mkini.net/v2/login-exchange")
                .method(Connection.Method.GET)
                .execute();

        String cookie = response.cookie("JSESSIONID");



        Document document = Jsoup.connect("http://www.malaysiakini.com/")
                .cookie("JSESSIONID", cookie)
                .get();

        Elements loginData = document.select("logindata");


        if(!loginData.isEmpty()){
            Log.d("Login Data "," Fail : " + loginData);
        }

        else
            Log.d("Login Data "," Success : " + loginData);


    } catch (IOException e) {
        e.printStackTrace();
    }

}

The warnings:

W/System.err: org.jsoup.HttpStatusException: HTTP error fetching URL.    Status=405, URL=http://www.malaysiakini.com/
W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:590)
W/System.err:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
W/System.err:     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
W/System.err:     at com.example.azrie.VoiceTheNews.BackgroundProcessLogin.login(BackgroundProcessLogin.java:60)
W/System.err:     at com.example.azrie.VoiceTheNews.BackgroundProcessLogin.doInBackground(BackgroundProcessLogin.java:41)
W/System.err:     at com.example.azrie.VoiceTheNews.BackgroundProcessLogin.doInBackground(BackgroundProcessLogin.java:20)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:841)

Edit

I changed the method from POST to GET and also take a look at the network but still I have no idea what I'm looking for, no idea whether I'm on the right track due to me very new to this. I included some screenshot for viewing.

enter image description here

enter image description here

azriebakri
  • 1,131
  • 2
  • 11
  • 36
  • Do you have all the appropriate permissions in the manifest file? – n1nsa1d00 Dec 09 '16 at 18:40
  • Thank you for replying! So far in my manifest I have `READ_EXTERNAL_STORAGE`, `WRITE_EXTERNAL_STORAGE`, and also `INTERNET`. Am I missing anything ? – azriebakri Dec 09 '16 at 18:47
  • You're not missing anything.. you're getting a 405 error in your request. I suggest to check your request setting and whether you can perform a log in that way... – n1nsa1d00 Dec 09 '16 at 19:02
  • Sorry for asking such a basic question, but how do I check my request setting ? I'm fairly new to android and java. – azriebakri Dec 09 '16 at 19:14
  • Wrong url for example, `http://login2.mkini.net/v2/login-exchange` is where you need to `Jsoup.connect(..` to. The simplest way to figure out the exact details of the login request is to use e.g. http://stackoverflow.com/questions/15603561/how-can-i-debug-a-http-post-in-chrome and look at an actually working http request (open network tab, then login, then inspect the request) – zapl Dec 09 '16 at 19:15
  • I updated the question. – azriebakri Dec 10 '16 at 05:01

1 Answers1

1

First you have to send a GET request to the site in order to get cookies. The cookie that you need later on is the _cfduid cookie. Next you have to send a POST request to a second URL - http://login2.mkini.net/v2/login-exchange with the _cfduid and some other values, as you can see here:
POST PARAMETERS

To obtain the URL, cookies, parameters and so, you can use your browser's developer tools and monitor the trafic between the browser and the server.
I've also noticed that you've tagged your question with Android tag, so you may want to use some tool that changes your browser's user-agent string, because the site may respond in a different way for mobiles.

TDG
  • 5,909
  • 3
  • 30
  • 51