-1

我正在开发一个 Android 应用程序(我的第一个),它显示桌面网站的移动友好界面。问题是网站的用户必须登录才能看到网站的这些部分。

我不拥有该网站,也不以任何方式与他们有任何关联,但我想为我自己的个人学习经验创建此应用程序,并帮助该网站的社区。

考虑到这一点,用于验证用户的登录表单可通过由 Invision Power Boards 提供支持的站点论坛访问(例如:community.invisionpower.com)。我知道如何从移动应用程序上的用户那里获取用户名和密码,但是如何将此信息发送到论坛/网站上的登录表单,然后以编程方式“单击”登录按钮?

4

1 回答 1

0

首先,您必须决定是在浏览器中还是在代码中执行此操作。

在代码中,您可能需要像这样自动化 HTTP 发布:

    // Start up your network connection
    URL url = new URL("http://www.asdf.com/someFormSubmissionPage);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoInput(true);

    String userInfo = "username=bob&password=pass";
    connection.setRequestProperty("Content-Length", "" + userInfo.length());

    connection.setDoOutput(true);

    // Send the actual data
    OutputStream out = connection.getOutputStream();
    out.write(userInfo.getBytes(UTF_8));

    // Flush everything to the destination
    out.flush();
    out.close();

    BufferedReader connectionReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));
    String responseLine;
    // Collect the entire response
    while ((responseLine = connectionReader.readLine()) != null)
        response.append(responseLine);

此时,您可以对响应做一些事情,也许保存服务器发回给您的 cookie,或者您想要的任何东西。

作为替代方案,您可以打开一个 WebView 并给它一个地址,例如http://www.asdf.com/someFormSubmissionPage?username=bob&password=pass

这将在用户登录的情况下启动浏览器。

The someFormSubmissionPage would be found out by doing a view source in a browser while looking at the login page for the actual site. Details of username/pass would also be found this way.

于 2013-03-01T06:19:27.747 回答