我是一个新手,对于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.点击“退出登录”链接
希望这样说得清楚。在互联网上寻找答案时,我只找到了“获取部分”的解决方案,所以这不是问题。但是我对“点击部分”毫无头绪。
任何帮助都将是极好的,谢谢!(如果我的英语不太好的话,请原谅...)
编辑:问题已解决。谢谢!
基本上,这个过程类似于从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());
}
.......