Android的:如何登录到网页编程,使用HttpsURLConnection [英] Android: How to login into webpage programmatically, using HttpsURLConnection

查看:120
本文介绍了Android的:如何登录到网页编程,使用HttpsURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid的(和Java中太),很抱歉,如果我的问题是一个基本的命题! 我必须写一个Android应用程序,whitch标志为背景的ASPX网页,从它那里得到一些数据,之后注销表格的网页。 (并且这样做,所有编程)

I'm new in Android (and in Java too), so sorry if my problem is a basic proposition! I have to write an Android app, whitch signs into an aspx webpage in the background, get some data from it, and after that logs out form the webpage. (and do that all programmatically)

基本上,该过程喜欢得到邮件列表从Gmail:
1 的去https://mail.google.com,并在
迹象 2 的点击联系人(==进入https://mail.google.com/mail/?shva=1&zx=dzi4xmuko5nz#contacts)
3。的提取使用页面HttpsURLConnection(或像这样),并获得一个(如地图或字符串)的电子邮件对象
4 的点击了注销链接

Basicly, the procedure likes getting email-list from Gmail:
1. go to 'https://mail.google.com', and signs in
2. click to the "Contacts" (== go to "https://mail.google.com/mail/?shva=1&zx=dzi4xmuko5nz#contacts")
3. fetch the page using HttpsURLConnection (or something like this), and get emails in an (e.g. Map or String) object
4. click to the "Sign out" link

我希望,这是可以理解的。展望互联网,我找了只有获取一部分的解决方案,所以这不是一个问题。但我没有关于点击一部分的想法。

I hope, it's understandable. Looking the internet, I find the solution for only the "fetching part", so that's not a problem. But I don't have any idea about the "clicking part".

  ......
    // 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
     */
  ......

任何帮助将是巨大的,谢谢大家了! (对不起,如果我的英语不是那么好...)

Any help would be great, Thank You for it! (and sorry, if my english isn't so well...)

编辑:问题就迎刃而解了。谢谢!

problem solved. Thank You!

 .......    
    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());
        }
 .......

推荐答案

点击基本上发送请求到服务器,并显示该返回信息。

"Clicking" is basically sending a request to a server and displaying the return informations.

1 /找出什么叫URL该请求(如果它是一个网页,看萤火虫为例)

1/ find out what url to call for that request (if it is a web page, see firebug for example)

2 /找出什么参数,找出如果方法是GET或POST

2/ find out what the parameters are, find out if the method is GET or POST

3 /再现编程。

4 /一个登录阶段可能意味着使用一个cookie,它的服务器给你,而且你必须发回之后为每个请求

4/ a "login" phase probably imply the use of a cookie, which the server gives you and that you must send back afterward for each request

不过,你的做法是错误的。你不应该试图直接通过登录URL连接到谷歌。 (你也应该使用HttpClient的)。此外,请求属性不是参数。他们的头。

However, your approach is wrong. You should not try to login directly to google via url connections. (Also you should use HttpClient). Moreover, request properties are not parameters. They are headers.

我强烈建议你开始的东西,以便更易于获得舒适与HTTP java开发,获取,岗位,参数,头,响应,饼干......

I strongly recommend you start with something simpler in order to get comfortable with HTTP in java, GETs, POSTs, parameters, headers, responses, cookies ...

在收到答复,你要检查

response.getStatusLine().getStatusCode() < 400

它会告诉你,登录成功。 (2XX的成功,3xx的移动而这种4XX都在请求错误,5XX的服务器端错误; Gmail的响应302登陆建议重定向到收件箱)。然后,你会发现,有在包含要进一步连接cookie中的反应设置Cookie一个特定的头这样:

It will tell you that login was successful. (2xx are success, 3xx are moved and such. 4xx are errors in the request, 5xx are server side errors ; Gmail responds 302 to login to suggest redirection to inbox). Then, you'll notice that there is a particular header in the response "Set-Cookie" that contains the cookie you want for further connections so :

String cookie = response.getFistHeader("Set-Cookie");

那么,你应该能够调用来获取联系人请求:

Then, you should be able to call the request to get the contacts :

HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();

这应该是类似的东西。

It should be something like that.

这篇关于Android的:如何登录到网页编程,使用HttpsURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆