Android:如何使用HttpsURLConnection以编程方式登录网页
我是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 nameValuePairs = new ArrayList(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,服务器为您提供,并且您必须在每次请求后发回
但是,你的方法是错误的。 您不应该尝试通过url连接直接登录谷歌。 (你也应该使用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();
它应该是那样的。
您要做的是解析Gmail html页面。 这是一种错误的方法,因为Gmail使用javascript来构建页面。 您的代码必须模拟浏览器(执行javascript)才能使用。
如果您只需要对Gmail的读取权限,请使用Gmail收件箱Feed API 。 这使您可以访问收件箱中的未读邮件。
如果您需要完全访问权限,请参阅Gmail IMAP访问权限 。 由于IMAP是一种不同的协议,因此您需要为Java使用单独的IMAP库。 请参阅本教程 。
您应该考虑使用post请求将数据传递到服务器: 在Android中发送POST数据
查找连接的属性与您想要实现的内容无关。
此致,Stéphane