1

我正在使用以下代码登录 Dropbox,但它会引发异常。

代码:

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://www.dropbox.com/login");

    // set values you'd like to send
    List pairs = new ArrayList();
    pairs.add(new BasicNameValuePair("login_email", "my email id"));
    pairs.add(new BasicNameValuePair("login_password", "******"));
    /*'login_email' and 'login_password' i found html form fields name by viewing the page source of https://www.dropbox.com/login */

    try {
        post.setEntity(new UrlEncodedFormEntity(pairs));
        // set ResponseHandler to handle String responses
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = client.execute(post, responseHandler);
        Log.v("HttpPost", "Response: " + response);
        if (response.contains("SUCCESS")){
            // express your joy here!
        } else {
            // pop a sad Toast message here...
        }
    } catch (Exception e) {
        Log.e("", String.valueOf(e));
    }
}

日志猫

         11-14 23:17:35.734: E/Trace(900): error opening trace file: No such file or directory (2)
         11-14 23:17:38.793: D/dalvikvm(900): GC_CONCURRENT freed 251K, 4% free 8194K/8519K, paused 126ms+9ms, total 234ms
         11-14 23:17:39.895: E/(900): org.apache.http.client.HttpResponseException: Forbidden
         11-14 23:17:40.364: I/Choreographer(900): Skipped 75 frames!  The application may be doing too much work on its main thread.
         11-14 23:17:40.394: D/gralloc_goldfish(900): Emulator without GPU emulation detected.
         11-14 23:17:40.784: I/Choreographer(900): Skipped 62 frames!  The application may be doing too much work on its main thread.
         11-14 23:17:43.614: W/chromium(900): external/chromium/net/disk_cache/backend_impl.cc:1829: [1114/231743:WARNING:backend_impl.cc(1829)] Destroying invalid entry.
4

2 回答 2

3
I/Choreographer(900): Skipped 75 frames!  The application may be doing too much work on its main thread.

始终建议使用AsyncTask在单独的线程而不是主线程上进行网络活动。

这是关于连接到网络的android教程

于 2012-11-14T18:20:04.620 回答
2

我尝试了您的来自 rest 插件的请求,并从服务器得到以下响应:

状态 403 禁止

<html>
<head><title>Dropbox - 403</title>
<link href="https://www.dropbox.com/static/css/main.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="/static/images/favicon.ico"/>
</head>
<body style="background-color:#fff">
<br/><br/>
<div align="center">
<table><tr><td width="600px">
<center><img id="errorimage" src="/static/images/lockbox.png"/></center>
<div id="errorbox">
<h1>Error (403)</h1>It seems you tried to do something we can't verify.  Did you log into a different Dropbox account in a different window?  Try clicking <a href="#" onclick="history.go(-1); return false;">here</a> to go back to the page you came from, or just go <a href="/home">home</a>.
</div>
</td></tr></table>
</div>

</body>
</html>

您用于登录的 Dropbox API 定义在哪里?

您应该尝试使用他们的其余 API 来进行以下链接中定义的集成:

https://www.dropbox.com/developers/reference/api

EDIT1:dropbx 也有可供您使用的移动平台的 sdk。检查此链接以获取 SDK。

EDIT2:刚刚检查了保管箱的登录页面。以下是登录表单中的相关 HTML:

<form action="https://www.dropbox.com/login" method="post"><input type="hidden" name="t" value="Hch4dxqGBwUN024pIuQbsxUQ" /><div class="sick-input small"><label for="login_email_elm">Email</label><input type="text" id="login_email_elm" name="login_email" tabindex="1" /></div><div class="sick-input small"><label for="login_password_elm">Password</label><input type="password" id="login_password_elm" name="login_password" tabindex="2" /></div><p style="margin-bottom: 0.5em; line-height: 28px;"><input style="vertical-align: middle; margin-left: 0;" name="remember_me" class="no-border" type="checkbox" id="remember_me" tabindex="3" /><label style="vertical-align: middle; cursor: pointer; " for="remember_me">Remember me</label><input tabindex="4" type="submit" class="freshbutton-blue" value="Sign in" id="toplogin-submit" /><input type="hidden" name="cont" value="https://www.dropbox.com/" /></p><p class="create-account"><a href="/register" id="login-create-an-account">Create an account</a></p></form>

它有两个带有自动生成值的隐藏参数t,我猜 Dropbox 使用它来阻止人们使用它来解释你使用它的原因。另一个是具有cont硬编码价值的https://www.dropbox.com/

任何方式我都尝试了这两个参数的请求,并且能够获得成功响应。以下是我使用 chrome 的其余插件的请求和响应的详细信息:

请求 URI:https://www.dropbox.com/login

请求内容类型:application/x-www-form-urlencoded

请求发布数据:

login_email=bscscomsats@gmail.com&login_password=yourpassword&t=Hch4dxqGBwUN024pIuQbsxUQ&cont=https://www.dropbox.com/

回应:To:https://www.dropbox.com/ with status: 302

响应标头:

Server: nginx/1.2.3 
Date: Wed, 14 Nov 2012 18:29:36 GMT 
Content-Type: text/html; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive
set-cookie: lid=AACt_twZtlN8Tz7TDINxpJFUFvG44oy3YAKZ7ht0umJMxA; Domain=www.dropbox.com; Path=/; secure; httponly
set-cookie: forumjar=eyJ2ZXJpZmllZCI6IDAsICJ1aWQiOiAxMjEwMjcwODYsICJleHBpcmVzIjogMTM1MzAwNDE3NiwgInBhaWQiOiAwLCAibG5hbWUiOiAiQ2xvdWRldHMiLCAiZm5hbWUiOiAiUG9ja2V0IiwgImRpc3BsYXlfbmFtZSI6ICJQb2NrZXQgQ2xvdWRldHMiLCAiZW1haWwiOiAiYnNjc2NvbXNhdHNAZ21haWwuY29tIn0%3D; Domain=dropbox.com; Path=/; httponly
set-cookie: blid=AACUWWo4_oPc00S7cf-xtgZ8dkkXXIkiyP8uQazNqQXjXw; Domain=dropbox.com; Path=/; secure; httponly
set-cookie: oscar=; Domain=www.dropbox.com; expires=Wed, 14-Nov-2012 18:29:36 GMT; Path=/; httponly
set-cookie: jar=W3sidWlkIjogMTIxMDI3MDg2LCAiaCI6ICJBQUNiSHJrUElzeWtiYUltak5MZWVnWE52OGYtRmxtZXJubXVXc0xnbnRlallRIiwgImV4cGlyZXMiOiAxMzUzMDA0MTc2LCAibnMiOiAxOTU2NzYxNjcsICJyZW1lbWJlciI6IGZhbHNlfV0%3D; Domain=www.dropbox.com; Path=/; secure; httponly
set-cookie: checkpoint=; Domain=www.dropbox.com; expires=Wed, 14-Nov-2012 18:29:36 GMT; Max-Age=0; Path=/; httponly
set-cookie: t=vmmVuSRbO6xqVJTJ9meSJVkf; Domain=dropbox.com; expires=Fri, 14-Dec-2012 18:29:36 GMT; Path=/; secure; httponly
set-cookie: grouch=; Domain=www.dropbox.com; expires=Wed, 14-Nov-2012 18:29:36 GMT; Path=/; httponly
set-cookie: bjar=W3sic2Vzc19pZCI6IDY3MDc1MTI0NDU3NDk5NzkwMTQ3OTg2NzExOTY1Njc4MDQ1NTA1LCAiZXhwaXJlcyI6IDEzNTMwMDQxNzYsICJ1aWQiOiAxMjEwMjcwODZ9XQ%3D%3D; Domain=dropbox.com; Path=/; secure; httponly
set-cookie: forumlid=NTM1NmJmYTU0NzQ2ZjgzMTNhZDI0YWE5ZjljZjZjNzViY2IxMzliYw%3D%3D; Domain=dropbox.com; Path=/; httponly
strict-transport-security: max-age=2592000; includeSubDomains
location: https://www.dropbox.com/
pragma: no-cache
cache-control: no-cache
x-frame-options: SAMEORIGIN

重定向响应:

Status
200 OK

响应头:

User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Content-Type: application/x-www-form-urlencoded 
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US
Accept-Charset: UTF-8,*;q=0.5
Cookie: gvc=NjcwNzUxMjQ0NTc0OTk3OTAxNDc5ODY3MTE5NjU2NzgwNDU1MDU%3D; t=Hch4dxqGBwUN024pIuQbsxUQ; __utma=74146836.1139928866.1352917086.1352917086.1352917086.1; __utmb=74146836.4.10.1352917086; __utmc=74146836; __utmz=74146836.1352917086.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)

可以在此pastebin链接中找到响应数据。

因此,由于t登录表单中的参数值是由 Dropbox 自动生成的,而且我肯定会在几分钟后过期,所以我建议不要使用这种方法进行登录。

于 2012-11-14T18:21:00.803 回答