0

为了清楚起见,我几乎没有 HTTP 方面的经验。这个项目对我来说雄心勃勃,但我愿意学习以便能够完成它。我已经在网上搜索了一些示例,但似乎找不到合适的解决方案。我知道 GET 和 POST 之类的术语,并且了解以编程方式与网站交互的基本方式。

基本上,我正在与之合作的公司有一个网站,其中包含我可以登录的客户数据库。对于初学者,我只想编写一个能够使用我的用户名和密码登录到主页的 Android 应用程序。该站点的登录 URL 为 https://"app.companysite.com"/Security/Login.aspx?ReturnUrl=%2fHome%2fDefault.aspx,并具有用于以下目的的证书:“确保远程计算机”。

我正在做的事情可能吗?最终,我希望能够打开一个客户页面并编辑他们的数据并重新提交,但一步一步。

如果你能指出一些相关的阅读材料或源代码的方向来帮助我实现我的目标,那就太棒了。

提前致谢!

4

1 回答 1

0

我不知道这是否有帮助,但我登录的方式只是为了证明。所以我所做的(因为我假设验证是通过 MySQL 数据库完成的)是创建一个 php 文件来验证登录用户名和密码是否正确并打印出“正确”或“是”,否则只是“否” ”或“无效”。像这样的东西:

php
//Connects to your Database

      $username = $_POST['username'];
      $password = $_POST['password'];

      //make your mysql query if the username and password are in the database
      //if there is a an approval
     $approval = 1 
     //otherwise
     $approval = 0

    if ($approval > 0) {
       echo "correct";
    } else {
       echo "invalid";
    }

    ?>

现在在 Android 中,您可以发出此请求来调用此网站并返回如下输出:

HttpParams httpParameters = new BasicHttpParams();
//make a timeout for the connections in milliseconds so 4000 = 4 seconds                    httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
int timeoutConnection = 4000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 4000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("your website URL");

// Add your data
String username = "your username";
String password = "your password";

List<NameValuePair> nameValuePairs;
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String lines = "";
String data = null;
ArrayList<String> al = new ArrayList<String>();

while((lines = in.readLine()) != null){
    data = lines.toString();
    al.add(data);
}
in.close();

//To get the response
if(al.get(0).equals("correct")){
      //Your login was successful
}
else {
    //Your login was unsuccessful
}

我希望这对您有所帮助,并为您指明了正确的方向。

于 2012-11-21T19:12:56.257 回答