Jsoup Login aspx It is not working correctly

151 Views Asked by At

Can you help me for login to aspx site with JSoup.

My Code;

package jsouplogin;

import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JSoupLogin {
    public static void main(String[] args) throws IOException {
         Connection.Response loginForm = Jsoup
        .connect("http://login.cu.edu.tr/Login.aspx?ReturnUrl=%2f")
        .method(Connection.Method.GET).execute();

         String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36";

         Document doc = loginForm.parse();

            String VIEWSTATEvalue = doc.select("input[name=__VIEWSTATE").attr("value");
            String EVENTTARGETvalue = doc.select("input[name=__EVENTTARGET").attr("value");
            String EVENTVARGUMENTvalue = doc.select("input[name=__EVENTARGUMENT").attr("value");
            String EVENTVALIDATIONvalue = doc.select("input[name=__EVENTVALIDATION").attr("value");
            String VIEWSTATEGENERATORvalue = doc.select("input[name=__VIEWSTATEGENERATOR").attr("value");                    

    doc = Jsoup.connect("http://login.cu.edu.tr/default.aspx")
            .referrer("http://www.google.com")
            .userAgent(userAgent)       
            .data("__EVENTTARGET", EVENTTARGETvalue)
            .data("__EVENTARGUMENT", EVENTVARGUMENTvalue)
            .data("__VIEWSTATE", VIEWSTATEvalue)
            .data("__VIEWSTATEGENERATOR", VIEWSTATEGENERATORvalue)
            .data("__EVENTVALIDATION", EVENTVALIDATIONvalue)           
            .data("ctl06$txtKullaniciAdi", "myuserid").data("ctl06$txtSifre", "mypassword")

        .cookies(loginForm.cookies()).post();

    System.out.println(doc);

    }

}

It is not login to site. My code will be load login form after run.

Sorry my english is very bad.

Web Page Source : view-source:http://login.cu.edu.tr/Login.aspx?ReturnUrl=%2f

1

There are 1 best solutions below

1
luksch On

Note: I have no Java compiler at hand right now, so I answer just from looking at your code.

To me it seems that the string definitions with the css selectors are wrong. Try to change

String VIEWSTATEvalue = doc.select("input[name=__VIEWSTATE").attr("value");

into

String VIEWSTATEvalue = doc.select("input[name=__VIEWSTATE]").attr("value");
                                                          ^

The closing brackets are missing in your string definition. Do this change for the other strings as well.

I am however not sure if this really solves your problem.