我是Android新手(也是Java),如果我的问题是一个基本命题,非常抱歉! 我必须编写一个Android应用程序,在后台登录aspx网页,从中获取一些数据,然后从网页中注销。 (和这样做,所有程序)Android:如何以编程方式登录到网页,使用HttpsURLConnection
基本上,该过程喜欢的Gmail电子邮件获取列表:
1.去 'https://mail.google.com',并在
2迹象。点击 “联系人”(==去 “https://mail.google.com/mail/?shva=1 & ZX = dzi4xmuko5nz#联系人”)
3.使用抓取网页HttpsURLConnection的(或类似的东西),并获得电子邮件(例如地图或字符串)对象
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());
}
.......
谢谢你的回答!遵循这一思路:上面的代码使用HttpClient,而GMAIL_LOGIN是url,它需要2个参数,email,pass;方法是POST。我认为,这是你的观点(1/2/3 /)。我的问题是,在这种情况下我怎么能得到Cookies,以及如何“浏览页面之间”?你能给我一些关于它的代码吗? (我知道我的知识对Java中的Http很差,但是接近完成此任务的时间,并且我被困住了;这就是为什么我要求帮助。) – Lama
我编辑我的帖子,其中包含有关第二个请求的信息 – njzk2
感谢代码!我尝试过(很多不同类型的),但仍然没有,我想要的。也许我错过了一些东西......我编辑第一个评论请提供代码,如果您有任何问题,请告诉我,错误! – Lama