javahtmlpostrequestjsoup

I have a POST request code which doesn't work for some reason


Sent all the hidden values, sent the correct login details, sent cookies and it still doesn't work. I've seen a few tutorials but I feel like the examples given there were little simple and also I am new to Jsoup.

import java.io.IOException;
import java.util.HashMap;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class example{
    public static void main( String[] args ) throws IOException{
        final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
        final String LOGIN_FORM_URL = "https://login.gitam.edu/Login.aspx";
        final String LOGIN_ACTION_URL = "https://login.gitam.edu/Login.aspx";
        final String USERNAME = "mysusername";
        final String PASSWORD = "mypassword";
        Connection.Response loginForm = Jsoup.connect(LOGIN_FORM_URL)
                .method(Connection.Method.GET)
                .userAgent(USER_AGENT)
                .execute();
        HashMap<String, String> cookies = new HashMap<>(loginForm.cookies());
        Document loginDoc = loginForm.parse();
        String viewstategenerator = loginDoc.getElementById("__VIEWSTATEGENERATOR").val();
        String eventtarget = loginDoc.getElementById("__EVENTTARGET").val();
        String eventargument = loginDoc.getElementById("__EVENTARGUMENT").val();
        String viewstate = loginDoc.getElementById("__VIEWSTATE").val();
        String eventvalidation = loginDoc.getElementById("__EVENTVALIDATION").val();
        HashMap<String, String> formData = new HashMap<>();
        formData.put("__VIEWSTATEGENERATOR", viewstategenerator);
        formData.put("__EVENTTARGET", eventtarget);
        formData.put("__EVENTARGUMENT", eventargument);
        formData.put("__VIEWSTATE", viewstate);
        formData.put("__EVENTVALIDATION", eventvalidation);
        formData.put("txtusername", USERNAME);
        formData.put("password", PASSWORD);
        formData.put("Submit", "Login");
        Connection.Response homePage = Jsoup.connect(LOGIN_ACTION_URL)
                .cookies(cookies)
                .data(formData)
                .method(Connection.Method.POST)
                .userAgent(USER_AGENT)
                .execute();



        System.out.println(homePage.parse().html());

    }
}

I've sent cookies, all other hidden values and I still don't get the output I want. (I just want the html source of after login page).


Solution

  • Here's how I made it working.

    Used requests and BeautifulSoup packages. Used session from requests. Session took care of all the cookies and headers and I was able to login without worrying about cookies and headers. I was then able to get any information from page post login.