如何使用jsoup,get-post方法和解析数据登录网站?

时间:2018-10-28 07:45:37

标签: java android post jsoup

我正在尝试从android应用登录并访问我的大学wifi门户。它是本地托管的,只有与他们的wifi连接时才能访问。我尝试了其他解决方案,但无法正常工作。登录HTML页面具有以下 form 标签。

表格:名称=“ sendin”

<form name="sendin" action="" method="post">
    <input type="hidden" name="username" id="mk_username" />
    <input type="hidden" name="password" id="mk_password" />
    <input type="hidden" id="mk_chapid" value="" />
    <input type="hidden" id="mk_error" value="" />
    <input type="hidden" id="mk_chapchallenge" value="" />
    <input type="hidden" name="dst" value="" />
    <input type="hidden" name="popup" value="false" />
    <input type="hidden" name="radius1-44115" value="12" />
</form>

表单:名称=“注销”

<form name="logout" action="" method="post">
    <input type="hidden" name="dst" value="" />
    <input type="hidden" name="popup" value="false" />
</form>

表单:名称=“输入”

<form name="login" action="" method="post" class="cd-form">
    <div class="imgcontainer">
        <h3>Login</h3>
    </div>
    <div class="container">
        <label><b>Username</b></label>
        <input type="text" class="form-control" id="cpusername" name="username" placeholder="Username" />
        <label><b>Password</b></label>
        <input type="password" class="form-control" id="cppassword" placeholder="Password" name="password" />
        <label class="checkbox text-left">
        <input type="checkbox" value="remember-me" id="chkrem" name="rememberMe" />
        <span>Remember Me</span>
        </label>
        <label class="checkbox text-left" style="display:none;">
        <input type="checkbox" value="remember-me" id="checkTerm" name="rememberMe" />
        I agree to the <a href="termAndCond.html" target="_blank">Terms & conditions.</a>
        </label>
    </div>
    <input type="submit" id="btnLogin" value="SUBMIT" onclick="javascript: return doLogin();" class="submit" />
</form>

我的代码

  package com.xenon.hellosmit;

  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;

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

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

  public class ionLogin extends AppCompatActivity {

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


          Button login = (Button) findViewById(R.id.button);

          login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {

                  EditText ionReg = (EditText) findViewById(R.id.ionregno);
                  EditText ionPas = (EditText) findViewById(R.id.ionpass);

                  try
                  {

                      Response loginPageResponse =
                              Jsoup.connect("http://115.124.113.97/SikkimCP/")
                                      .referrer("http://115.124.113.97/")
                                      //.timeout(10 * 1000)
                                      .userAgent("Mozilla/5.0")
                                      .method(Connection.Method.GET)
                                      .followRedirects(true)
                                      .execute();

                      System.out.println("Fetched login page");

                      //get the cookies from the response, which we will post to the action URL
                      Map<String, String> mapLoginPageCookies = loginPageResponse.cookies();

                      Map<String, String> mapParams = new HashMap<String, String>();
                      mapParams.put("username", ionReg.getText().toString());
                      mapParams.put("password", ionPas.getText().toString());
                      mapParams.put("rememberMe", "remember-me");
                      //mapParams.put("btnLogin", "SUBMIT");

                      //URL found in form's action attribute
                      String strActionURL = "http://115.124.113.97/SikkimCP/";

                      Response responsePostLogin = Jsoup.connect(strActionURL)
                              //referrer will be the login page's URL
                              .referrer("http://115.124.113.97/SikkimCP/")
                              //user agent
                              .userAgent("Mozilla/5.0")
                              //connect and read time out
                              //.timeout(10 * 1000)
                              //post parameters
                              .data(mapParams)
                              //cookies received from login page
                              .cookies(mapLoginPageCookies)
                              //many websites redirects the user after login, so follow them
                              .followRedirects(true)
                              .method(Connection.Method.POST)
                              .cookies(loginPageResponse.cookies())
                              .execute();


                      System.out.println("HTTP Status Code: " + responsePostLogin.statusCode());

                      //parse the document from response
                      Document document = responsePostLogin.parse();
                      System.out.println(document);

                      //get the cookies
                      Map<String, String> mapLoggedInCookies = responsePostLogin.cookies();

                      Intent loginSuccess = new Intent(ionLogin.this, ionSuccess.class);
                      startActivity(loginSuccess);
                  }

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


      }
  }

因此,我想登录wifi并解析数据以将其显示在我的android应用中。我是android的初学者,只有一点Java经验。

0 个答案:

没有答案