我是 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());
}
.......
最佳答案
“点击”基本上是向服务器发送请求并显示返回信息。
1/找出该请求调用的 url(如果是网页,例如查看 firebug)
2/看参数是什么,看方法是GET还是POST
3/以编程方式复制。
4/“登录”阶段可能意味着使用 cookie,服务器会为您提供该 cookie,您必须在每次请求后发回该 cookie
但是,你的做法是错误的。您不应该尝试通过 url 连接直接登录到 google。 (你也应该使用 HttpClient)。此外,请求属性不是参数。它们是标题。
我强烈建议您从更简单的东西开始,以便熟悉 java 中的 HTTP、GET、POST、参数、 header 、响应、cookie ...
编辑
收到回复后,您需要检查一下
response.getStatusLine().getStatusCode() < 400
它会告诉你登录成功。 (2xx 是成功,3xx 是移动等。4xx 是请求中的错误,5xx 是服务器端错误;Gmail 响应 302 登录以建议重定向到收件箱)。然后,您会注意到响应“Set-Cookie”中有一个特定的 header ,其中包含您想要用于进一步连接的 cookie,因此:
String cookie = response.getFistHeader("Set-Cookie");
然后,您应该能够调用请求以获取联系人:
HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();
应该是这样的
关于java - Android:如何使用 HttpsURLConnection 以编程方式登录网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7175238/