简体   繁体   English

Android —如何以编程方式从表单登录网站

[英]Android — How to programmatically log into website from a form

The app I am writing involves taking details from the user and using them to access their account on a website. 我正在编写的应用程序涉及从用户那里获取详细信息,并使用他们访问网站上的帐户。 I know how to take details from the user but I don't know how to use those details to log into the website. 我知道如何从用户那里获取详细信息,但是我不知道如何使用这些详细信息登录网站。 Does anyone know how I can input these details in the correct boxes? 有谁知道我该如何在正确的框中输入这些详细信息? Thanks in advance 提前致谢

You should use a Webview to display to webpage. 您应该使用Webview来显示网页。 Check out the official example from Google: http://developer.android.com/resources/tutorials/views/hello-webview.html 查看来自Google的官方示例: http : //developer.android.com/resources/tutorials/views/hello-webview.html

To fill the correct fields, check this answer: Fill fields in webview automatically 要填写正确的字段,请检查以下答案: 在Webview中自动填写字段

First of all, you should check out if the website has the API. 首先,您应该检查网站是否具有API。

If they don't have the API, 如果他们没有API,

  1. In Firebug (in Firefox) or Developer Tools (in Chrome), goto Network Tab and see what POST is done. 在Firebug(在Firefox中)或Developer Developer(在Chrome中)中,转到“网络”选项卡,然后查看完成的POST。
  2. Now that you know what is POSTed & to what URL, you have to do that using Java. 现在您知道什么是POST和什么URL,您必须使用Java来完成。
  3. While doing the HTTP POST/GET requests in Java, you will also need to handle cookies (which your browser does automagically for you). 在用Java执行HTTP POST / GET请求时,您还需要处理cookie(浏览器会自动为您处理)。 For that you need something called a CookieJar . 为此,您需要一个称为CookieJar的东西。 You can use that without using a cookiejar too as explained here but it's cumbersome. 您可以在不使用cookiejar的情况下使用它但这很麻烦。

So, read a tutorial here & get started . 因此,请在此处阅读教程并开始使用

If the website is using POST, which is the most likely case, you can simply use UrlEncodedFormEntity and HttpClient to do that 如果网站使用的是POST(最有可能的情况),则只需使用UrlEncodedFormEntityHttpClient即可

HttpParams params = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost("www.thewebsiteyouwanttosubmitto.com/post.php");
post.setEntity(new UrlEncodedFormEntity());
client.execute(post, new ResponseHandler(){});

Use Bookmarklets , read the source code of the login page and figure our which variables are used to Actually store the variables, for example if the 'id' for user name was 'userName' and the 'name' password was 'pwd' you could access them using 使用Bookmarklets ,阅读登录页面的源代码,并弄清楚我们实际使用了哪些变量来存储变量,例如,如果用户名的'id'是'userName'而'name'密码是'pwd',则可以使用访问他们

document.getElementById('userName').value

and

document.getElementsByName(\"pwd\")[0].value

if the login is a little more roundabout, like in the case of sonicwall (which i am trying to automate) these variables might be hidden, then you could use 如果登录时要多一些回旋处,例如在sonicwall(我要使其自动化)的情况下,这些变量可能会被隐藏,那么您可以使用

document.standardPass.uName.value

This is some parts of the webpage for which the above codes was used, 这是使用上述代码的网页的某些部分,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
<form name="standardPass" onsubmit="return(processButn());" action="auth.cgi" method="POST" target="authTgtFrm">

    <div id="login_box" align="left">
        <div class="vgap85"></div>
        <div id="error_box" style="visibility:hidden;">
            <div id="error_text">
            </div>
        </div>
        <div id="admin_reauth_text" style="visibility:hidden;">
            Please enter your password to begin management:
        </div>
        <div id="username_line">
            <div class="fieldName">
                Username:
            </div>
            <div class="fieldValue">
                <input type="text" id="userName" name="userName" value="" maxlength="32" autocomplete="off" style="width: 180px;">
            </div>
        </div>
        <div class="vgap8"></div>
        <div id="password_line">
            <div class="fieldName">
                Password:
            </div>
            <div class="fieldValue">
                <input type="password" name="pwd" value="" maxlength="63" autocomplete="off" style="width: 180px;">
            </div>
        </div>
        <div class="vgap8"></div>
        <div id="language_line">
            <div class="fieldName">
                Language:
            </div>
            <div class="fieldValue">
                <select name="select2">
                    <option>English</option>
                </select>
            </div>
        </div>
        <div class="vgap15"></div>
        <div id="button_line">
            <div class="fieldName">
                &nbsp;
            </div>
            <div class="fieldValue">
                <input type="submit" name="Submit" value="Login" class="button" >
            </div>
        </div>
        <div class="vgap8"></div>
        <div id="sslvpn_enabled" style="visibility:hidden;">
            <div class="fieldName">
                &nbsp;
            </div>
            <div class="fieldValue">
                Click <a href="sslvpn" onClick="top.location.href='sslvpn'";>here</a> for sslvpn login
            </div>
        </div>
    </div>

    <input type="hidden" name="uName">
    <input type="hidden" name="pass">
    <input type="hidden" name="digest">
</form>
<noscript>
    <center>
        <font color="#FFFFFF"><font size="+1">Please turn on JavaScript to login.</font></font>
    </center>
</noscript>

Now in android use webview to load the authentication page then load your bookmarklet on your webview. 现在在android中使用webview加载身份验证页面,然后将您的bookmarklet加载到webview上。 Test out your bookmarklet in your pc before trying in the webview. 在尝试使用Webview之前,先在PC中测试您的Bookmarklet。 Make sure the variables are accessible in that website using your browser console(The original authentication website might be something else ( Frames 确保使用浏览器控制台可以在该网站中访问变量(原始身份验证网站可能是其他网站( 框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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