代码之家  ›  专栏  ›  技术社区  ›  Hwuiwon Kim

如何使用JSOUP发布表单登录?[副本]

  •  1
  • Hwuiwon Kim  · 技术社区  · 9 年前

    https://spps.getalma.com

    以下是表单源

    <form id="login-form" action="/login" method="post" autocomplete="off" class="pure-form pure-form-stacked login-form">
        <label>
            Username
            <input name="username" type="text" class="pure-input-1" placeholder="Username" value="" required="" autofocus="">
        </label>
    
        <label>
            Password
            <input name="password" type="password" class="pure-input-1" placeholder="Password" value="" required="">
        </label>
        <button type="submit" class="pure-button pure-button-primary">Login</button>
    </form>
    

    我在试这个

    public String getString(String username, String password) {
        Connection.Response loginForm = null;
        Document document = null;
        try {
            loginForm = Jsoup.connect("https://spps.getalma.com/").method(Connection.Method.GET).execute();
            document = Jsoup.connect("https://spps.getalma.com/login")
                    .data("cookieexists", "false").data("username", username)
                    .data("password", password).cookies(loginForm.cookies()).post();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        String temp = document.title();
        return temp;
    }
    

    我猜它没有正确登录,我不知道该怎么做才能让它工作。。

    您可以使用此用户名和密码登录

    密码: Dvjpp;[sddeptfdr,rdyrt1

    android.os.NetworkOnMainThreadException

    请帮帮我。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Rajesh N    9 年前

    用这些方法在课堂下打电话

        new AsyncCaller().execute();
    
     private class AsyncCaller extends AsyncTask<Void, Void, String>
        {
            ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
    
                //this method will be running on UI thread
                pdLoading.setMessage("Loading...");
                pdLoading.show();
            }
            @Override
            protected String doInBackground(Void... params) {
    
               String result= getString("your username","your password")
    
    
                return result;
            }
    
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
             pdLoading.dismiss();
            Toast.make(context,result,Toas.LENGTH_SHORT).show();
            }
    
            }
        }