我是Android的新手(也是Java),如果我的问题是一个基本命题,那就很抱歉!
我必须编写一个Android应用程序,在后台登录到aspx网页,从中获取一些数据,然后从网页中注销. (以编程方式完成所有操作)
基本上,该过程喜欢从Gmail获取电子邮件列表:
1.转到“https://mail.google.com”,然后登录
2.点击“联系人”(==转到“https://mail.google.com/mail/?shva=1\u0026amp;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,服务器为您提供,并且您必须在每次请求后发回
但是,你的方法是错误的.您不应该尝试通过网址连接直接登录谷歌. (你也应该使用HttpClient).而且,请求属性不是参数.它们是标题.
我强烈建议你从简单的东西开始,以便在java,GET,POST,参数,标题,响应,cookie中熟悉HTTP …
编辑
收到回复后,您需要检查一下
response.getStatusLine().getStatusCode() < 400
它会告诉您登录成功. (2xx成功,3xx被移动等等.4xx是请求中的错误,5xx是服务器端错误; Gmail响应302登录以建议重定向到收件箱).然后,您会注意到响应“Set-Cookie”中有一个特定的标题,其中包含您想要进一步连接的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();
它应该是那样的.