以编程方式登录到Android网站 [英] Logging in to website programmatically Android

查看:65
本文介绍了以编程方式登录到Android网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要以编程方式登录网站的应用程序.我尝试使用此代码,但没有使我登录.

  @Override受保护的布尔值doInBackground(Void ... arg0){尝试 {Connection.Response res = Jsoup.connect("http://omegastrike.co.uk/member.php?action=login").data(用户名",用户名,密码",密码).followRedirects(true).method(Method.POST).执行();Map< String,String>Cookies = res.cookies();文档doc2 = Jsoup.connect("http://omegastrike.co.uk/index.php").cookies(cookies).得到();System.out.println(doc2);} catch(IOException e){//TODO自动生成的catch块e.printStackTrace();}返回null;} 

奖金问题:如何在应用程序的其他功能中使用此登录连接?我需要继续登录吗?

解决方案

因此,当您查看此页面的登录表单时,原始HTML如下所示:

 < form method ="post" action ="member.php">< table border ="0" width ="100%">< tr>< td>< label for ="login_username">用户名:</label></td>< td>< input type ="text" value =" style ="width:95%;"maxlength ="30" size ="25" name =用户名" class =文本框" id =登录用户名"/></td></tr>< tr>< td>< label for ="login_password">密码:</label></td>< td>< input type ="password" value =" style ="width:95%;"size ="25" name ="password" class ="textbox" id ="login_password"/></td></tr>< tr>< td>< label class ="smalltext" title =如果选中,将在此计算机上记住您的登录详细信息,否则,关闭浏览器后,您将立即注销.">< input type ="checkbox"value ="yes" check ="checked" name =记住" class ="checkbox">记住我</label></td>< td style ="text-align:right;"><输入type ="submit" value =登录" name ="submit" id ="button_postbit"/></td></tr></table><输入type ="hidden" value ="do_login" name ="action"/><输入type ="hidden" value =" name ="url"/></form> 

如您所见,这是URL http://omegastrike.co.uk/member.php POST .提交了多个字段,而不仅仅是用户名和密码.字段是:

  [用户名] =>在这里命名[密码] =>通过这里[记住] =>是的[提交] =>登录[action] =>do_login 

因此您需要在POST请求中包括所有这些内容.

它看起来像这样:

  Connection.Response res = Jsoup.connect("http://omegastrike.co.uk/member.php").data(用户名",用户名,密码",密码,提交",登录",操作","do_login").followRedirects(true).method(Method.POST).执行(); 

关于保持登录状态,我没有要测试的帐户,但是通常在登录时设置了会话ID标头或cookie,如果随后的请求中包含该ID,将使您保持登录状态./p>

I am creating an app which needs to login to a website programmatically. I tried to use this code, but it doesn't log me in.

@Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Connection.Response res = Jsoup.connect("http://omegastrike.co.uk/member.php?action=login")
                    .data("username", username, "password", password)
                    .followRedirects(true)
                    .method(Method.POST)
                    .execute();
            
            Map<String, String> cookies = res.cookies();

            Document doc2 = Jsoup.connect("http://omegastrike.co.uk/index.php")
                    .cookies(cookies)
                    .get();
            

            System.out.println(doc2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

Bonus question: How do I use this logged in connection for other functionalities in the app? Do I need to keep logging in?

解决方案

So when you look at the login form for this page, the raw HTML looks like this:

    <form method="post" action="member.php">
        <table border="0" width="100%">
            <tr>
                <td>
                    <label for="login_username">Username:</label>
                </td>
                <td>
                    <input type="text" value="" style="width: 95%;" maxlength="30" size="25" name="username" class="textbox" id="login_username" />
                </td>
            </tr>
            <tr>
                <td>
                    <label for="login_password">Password:</label>
                </td>
                <td>
                    <input type="password" value="" style="width: 95%;" size="25" name="password" class="textbox" id="login_password" />
                </td>
            </tr>
            <tr>
                <td>
                    <label class="smalltext" title="If ticked, your login details will be remembered on this computer, otherwise, you will be logged out as soon as you close your browser."><input type="checkbox" value="yes" checked="checked" name="remember" class="checkbox"> Remember Me</label>
                </td>
                <td style="text-align: right;">
                    <input type="submit" value="Login" name="submit" id="button_postbit" />
                </td>
            </tr>
        </table>
        <input type="hidden" value="do_login" name="action" />
        <input type="hidden" value="" name="url" />
    </form>

As you can see, this is a POST, to the URL http://omegastrike.co.uk/member.php. There are several fields being submitted, not just username and password. The fields are:

    [username] => namehere 
    [password] => passhere 
    [remember] => yes 
    [submit] => Login 
    [action] => do_login

So you need to include all of those in your POST request.

It would look something like this:

    Connection.Response res = Jsoup.connect("http://omegastrike.co.uk/member.php")
                .data("username", username, "password", password, "submit", "Login", "action", "do_login")
                .followRedirects(true)
                .method(Method.POST)
                .execute();

As to staying logged in, I don't have an account with which to test, but generally there is a session id header or a cookie set upon login, that if included with subsequent requests, will keep you logged in.

这篇关于以编程方式登录到Android网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆