简体   繁体   中英

How to Automate login to a webpage using jsoup in android?

I was trying to make an automatic login for my users to http://www.bvrit.edu.in using jsoup and the display the logged in web page for my users using a webview.I added the jsoup API,checked using inspect elements that the id of the usename field is txtId1 and password is txtPwd1 and replace data in post with the respective names.I also added the internet access permission to manifest but i am not able to display the webpage my code is as shown below,i think i am getting some basics wrong but had not been able to figure it out.

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void main(String[] args) throws IOException {

    WebView browser = (WebView) findViewById(R.id.bvritWebview);

    Connection.Response loginForm = Jsoup.connect("http://www.bvrit.edu.in/")
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .referrer("http://www.google.com")
            .timeout(12000)
            .followRedirects(true)
            .method(Connection.Method.GET)
            .execute();

    Connection.Response loginFormFilled = Jsoup.connect("http://www.bvrit.edu.in/")
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .followRedirects(true)
            .referrer("https://login.to/")
            .data("txtId1", "username")//check the form to find field name for user name
            .data("txtPwd1", "password")//check the form to find field name for user password
            .cookies(loginForm.cookies())
            .method(Connection.Method.POST)
            .execute();
    int statusCode = loginFormFilled.statusCode();
    Map<String, String> cookies = loginFormFilled.cookies();
    browser.getSettings().setJavaScriptEnabled(true);
    browser.loadUrl("http://www.bvrit.edu.in");

}

}

header section of the networking after logigng in- 在此处输入图片说明

You are missing few parameters in your POST request. Load the page in your browser, press F12 to launch the developer tools and look at the POST request. You will see something like this -
POST请求

You must send all these parameters to the server, not just those you do send now.
The first 3 parameters are unique to each session, and you can get them form the first GET request, something like this (the CSS selector may be different, I didn't try it on your URL):

Document doc = loginForm.parse();
Element e = doc.select("input[id=__VIEWSTATE]").first();
String viewState = e.attr("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM