【问题标题】:Get Metadata from Dropbox Link Without Auth无需身份验证从 Dropbox 链接获取元数据
【发布时间】:2013-09-03 15:39:08
【问题描述】:

我想通过 Dropbox 上的共享链接检查版本更改/获取文本文件的元数据。我不会使用 dropbox api,因为它让用户使用自己的帐户。我希望他们链接到我的帐户,但我不能手动这样做,因为我以后可能会更改我的密码。

所以:没有身份验证令牌,只需从保管箱的共享链接获取元数据,以便我可以检查版本更改,如果版本更改,请下载新文件的内容。

另外:我愿意接受其他建议以使这项工作也有效。请详细说明您的解决方案。

更新的电子标签问题:

public void getFromOnlineTxtDatabase(){
        try{
            URL url = new URL("url-here");
            HttpURLConnection.setFollowRedirects(true);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(false);
            con.setReadTimeout(20000);
            con.setRequestProperty("Connection", "keep-alive");
            //get etag for update check
                String etag = con.getHeaderField("etag");
            //String etag= "";

            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
            ((HttpURLConnection) con).setRequestMethod("GET");
            //System.out.println(con.getContentLength()) ;
            con.setConnectTimeout(5000);
            BufferedInputStream in = new BufferedInputStream(con.getInputStream());
            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println(responseCode);
            }
            StringBuffer buffer = new StringBuffer();
            int chars_read;
            //int total = 0;
            while ((chars_read = in.read()) != -1) 
            {
                char g = (char) chars_read;
                buffer.append(g);
            }
            final String page = buffer.toString();
            //create password_ems.txt to internal
            if (fileExistance("data.txt")){
                File dir = getFilesDir();
                File file = new File(dir, "data.txt");
                boolean deleted = file.delete();
                stringToTxt(page, "data.txt");


            }else{
                stringToTxt(page, "data.txt");
            }

            if (fileExistance("data_etag.txt")){
                File dir = getFilesDir();
                File file = new File(dir, "etag.txt");
                boolean deleted = file.delete();
                stringToTxt(etag, "etag.txt");


            }else{
                //create etag_file
                stringToTxt(etag, "data_etag.txt");
            }

            //  Log.i("Page", page);
        }catch(Exception e){
            showDialog("Database Fetch Failure","Unable to Fetch Password Database, check your internet" +
                    " connection and try again later.",0);
            Log.i("Page", "Error");
        }

    }

【问题讨论】:

    标签: java android sync dropbox dropbox-api


    【解决方案1】:

    如果您对公共或共享 Dropbox URL 执行 HTTP HEAD 请求,您将获得 etag 标头等。我不知道这种行为是否得到保证,因为我认为它没有在任何地方记录,但至少现在etag 标头可用于确定文件何时更改。 (如果etag不同,则文件已更改。)

    编辑

    通常在使用 ETag 时,最有效的做法是发出带有 If-None-Match: <old etag> 标头的 GET 请求。如果内容没有改变,它会以 304 响应,但如果内容改变了,它将按照正常的GET 请求下载新内容(响应为 200)。

    【讨论】:

    • @smarx- 感谢您的回答。我是在 Dropbox url 上执行 HTTP HEAD 请求,其中 txt 文件位于 Dropbox 布局“内部”,还是浏览器将 txt 文件显示为纯 txt 的 url,没有任何 Dropbox '从属关系'。我有两个网址。
    • 后者。在指向实际内容的链接上执行此操作。
    • @smarx- 当我查看源代码时,它只是文本。你确定头部会工作吗?
    • ETag 通常被浏览器用于仅在数据发生更改时才请求数据,因此在内容发生变化时依赖 ETag 更改是非常安全的。 (如果这没有发生,浏览器将错误地缓存陈旧的数据。)至于您的代码不起作用,很难猜出哪里出了问题,因为您没有共享代码或异常。 :-) 我确实注意到您使用的是大写“ETag”,而 Dropbox 服务器似乎发出“etag”(小写)。这可能是个问题。
    • 您打算做的是 1) 获取 ETag,然后 2) 如果 ETag 不匹配,则获取新内容。那是两个网络请求。我提议的是 1) 使用 If-None-Match: 请求新内容,只需一个 Web 请求。如果 ETag 不匹配,服务器将向您发送新内容,或者如果 ETag 匹配,它只会回复 304(未修改)。但首先只是在尝试优化之前尝试让某些东西工作。 :-)
    猜你喜欢
    • 2019-01-28
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    相关资源
    最近更新 更多