如何制作一个Android登录应用程序


How To Make an Android Login App?

我有一个任务,使android应用程序能够访问需要登录的页面。我没有访问该网站使用的php代码。这是网站的HTML来源:

<center>
    <div id="loginWrapper">
        <div id="loginBox">
            <div id="loginBox-head"></div>
            <div id="loginBox-title">
                <h1></h1>
            </div>
            <div id="loginBox-body">
                <form id="form-login" action="https://xxxxx.xxxxxx.xxx/index.php?pModule=bG9naW4=&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=" method="post" autocomplete="off">
                    <label for="aid_username">Username</label>
                    <input type="text" id="username" name="username" value="" onBlur="clearPasswordField();" />
                    <label for="aid_password">Password</label>
                    <input type="password" id="password" name="password" value="" />
                    <label> </label>
                    <input type="submit" value="Login" />
                    <hr />
                </form>
            </div>
            <div id="loginBox-foot"></div>
        </div>
    </div>
</center>

我看到https://xxxxx.xxxxxx.xxx/index.php?pMod…JvY2Nlc3M=是它走的方式,如果我点击登录按钮。当我访问那个URL时,它回应我需要输入用户名和密码。我试图将用户名和密码传递到URL,所以它会像这样:https://xxxxx.xxxxxx.xxx/index.php?pMod…Fzc3dvcmQ=,它仍然响应我需要输入它。因此,我知道它通过套接字参数传递用户名和密码。我尝试使用firebug (firefox插件)并手动登录它,结果如下:

POST index.php?pModule=bG9naW4=&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=
Params
-----------
pAct   cHJvY2Nlc3M=
pModule   bG9naW4=
pSub   bG9naW4=
Headers
-------------
Response Headers
Server   nginx/0.7.64
Date   Wed, 14 Dec 2011 19:56:32 GMT
Content-Type   text/html
Transfer-Encoding   chunked
Connection   keep-alive
X-Powered-By   PHP/4.4.8
Expires   Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma   no-cache
Location   https://xxxxx.xxxxxx.xxx/index.php?pModule=aG9tZQ==&pSub=aG9tZQ==&pAct=dmlldw==
Content-Encoding   gzip
Vary   Accept-Encoding
Request Headers
Host   xxxxx.xxxxxx.xxx
User-Agent   Mozilla/5.0 (X11; Linux i686; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
Accept   text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language   en-us,en;q=0.5
Accept-Encoding   gzip, deflate
Accept-Charset   ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT   1
Connection   keep-alive
Referer   https://xxxxx.xxxxxx.xxx/
Cookie   PHPSESSID=ecf5337e2f08bf48537da099b90b8a3f
Post
--------
Parameters
password xxxxxx
username xxxxxx
source
Content-Type: application/x-www-form-urlencoded
Content-Length: 31
username=xxxxxx&password=xxxxxx
Response & HTML
-----------------------------
Reload the page to get source for: https://xxxxx.xxxxxx.xxx/index.php?pModule=bG9naW4=&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=

我尝试了两个不同的代码,它失败了。

private void login1(){
    BufferedReader in = null;
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(2);
    postParameters.add(new BasicNameValuePair("username", user.getText().toString()));
    postParameters.add(new BasicNameValuePair("password", pass.getText().toString()));
    try {
        HttpClient client = new DefaultHttpClient();            
        HttpPost request = new HttpPost("https://xxxxx.xxxxxx.xxx/index.php?pModule=bG9naW4=&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=");
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(entity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder buffer = new StringBuilder();
        String line = "";
        String nl = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            buffer.append(line + nl);
        }
        in.close();
        TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setText(buffer.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

privated void Login2()
{           
    HttpURLConnection connection;
    OutputStreamWriter request = null;
        URL url = null;          
        String parameters = "username=" + user.getText().toString() + "&password=" + pass.getText().toString();   
        try
        {
            url = new URL("https://xxxxx.xxxxxx.xxx/index.php?pModule=bG9naW4=&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.addRequestProperty("Content-Length", Integer.toString(parameters.length()));
            connection.setRequestMethod("POST");    
            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();            
            String line = "";               
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "'n");
            }           
            TextView txt = (TextView) findViewById(R.id.textView1);
            txt.setText(sb.toString());        
            isr.close();
            reader.close();
        }
        catch(IOException e)
        {
            // Error
        }
}
你知道我错在哪里吗?

谢谢你的帮助。对不起,我的英语不好。

尝试将您的查询作为GET发送,这取决于服务器的编码方式,它可能工作:

https://xxxxx.xxxxxx.xxx/index.php?pModule=bG9naW4=&username=USERNAME&password=PASSWORD&pSub=bG9naW4=&pAct=cHJvY2Nlc3M=

USERNAMEPASSWORD替换为您想要登录的任何凭据