我是 Android 新手(也是 Java 新手),如果我的问题是一个基本命题,那么很抱歉!
我必须编写一个 Android 应用程序,在后台登录一个 aspx 网页,从中获取一些数据,然后从该网页注销。 (并以编程方式完成这一切)
基本上,该过程就像从 Gmail 获取电子邮件列表:
1.转到“https://mail.google.com”,然后登录
2.点击“联系人”(==转到“https://mail.google.com/mail/?shva=1&zx=dzi4xmuko5nz#contacts”)
3.使用 HttpsURLConnection (或类似的东西)获取页面,并在(例如 Map 或 String)对象中获取电子邮件
4.单击“退出”链接
我希望,这是可以理解的。在互联网上查找,我找到了仅“获取部分”的解决方案,所以这不是问题。但我对“点击部分”没有任何想法。
......
// Get the connection
URL myurl = new URL("https://mail.google.com");
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
// complete the fields
con.setRequestProperty("Email","myacc");
con.setRequestProperty("Passwd","mypass");
/*
* in this part, should make sign in, and go directly to contacts...
* I don't have any idea how to do it...
*/
// for the present, just write out the data
InputStream ins = con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.d("Page:"," "+inputLine);
}
in.close();
/*
* And here should be the "Sign out" part
*/
......
任何帮助都会很棒,谢谢!
(抱歉,如果我的英语不太好......)
编辑:问题已解决。谢谢你!
.......
String GMAIL_CONTACTS = "https://mail.google.com/mail/?shva=1#contacts";
String GMAIL_LOGIN = "https://mail.google.com";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(GMAIL_LOGIN);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Email", MY_ACC));
nameValuePairs.add(new BasicNameValuePair("Passwd", MY_PASS));
nameValuePairs.add(new BasicNameValuePair("signIn", "Sign In"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
Log.d(TAG, "response stat code " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() < 400) {
String cookie = response.getFirstHeader("Set-Cookie")
.getValue();
Log.d(TAG, "cookie: " + cookie);
// get the contacts page
HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
ins));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.d(TAG, " " + inputLine);
}
in.close();
} else {
Log.d(TAG, "Response error: "
+ response.getStatusLine().getStatusCode());
}
.......