首先,您必须决定是在浏览器中还是在代码中执行此操作。
在代码中,您可能需要像这样自动化 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.