从Dropbox的链接获取元数据无需身份验证 [英] Get Metadata from Dropbox Link Without Auth

查看:100
本文介绍了从Dropbox的链接获取元数据无需身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查一个版本的改变/获得一个文本文件的元数据与Dropbox的共享链接。我不会使用Dropbox的API,它使用户能够使用他们自己的帐户。我希望他们能够链接到我的帐户,我不能这样做手工,因为我以后可能会改变我的密码。

这样:没有身份验证令牌,刚刚获得元数据从Dropbox的共享链接,这样我可以检查版本的变化,如果版本已更改下载新文件的内容

也:我接受其他的建议,使这项工作为好。请稍后详细说明您的解决方案。

更新电子标签问题:

 公共无效getFromOnlineTxtDatabase(){
        尝试{
            网址URL =新的URL(URL-这里);
            HttpURLConnection.setFollowRedirects(真正的);
            HttpURLConnection的CON =(HttpURLConnection类)url.openConnection();
            con.setDoOutput(假);
            con.setReadTimeout(20000);
            con.setRequestProperty(连接,保持活动);
            //获取ETAG进行更新检查
                串的etag = con.getHeaderField(ETAG);
            //字符串ETAG =;

            con.setRequestProperty(用户代理,Mozilla的/ 5.0(Windows NT的6.1; WOW64; RV:16.0)的Gecko / 20100101火狐/ 16.0);
            ((HttpURLConnection类)CON).setRequestMethod(GET);
            //System.out.println(con.getContentLength());
            con.setConnectTimeout(5000);
            的BufferedInputStream在=新的BufferedInputStream(con.getInputStream());
            INT响应code = con.getResponse code();
            如果(响应code == HttpURLConnection.HTTP_OK){
                的System.out.println(响应code);
            }
            StringBuffer的缓冲区=新的StringBuffer();
            INT chars_read;
            // INT总= 0;
            而((chars_read = in.read())!=  -  1)
            {
                焦炭G =(焦炭)chars_read;
                buffer.append(G);
            }
            最后弦乐页= buffer.toString();
            //创建password_ems.txt内部
            如果(fileExistance(data.txt中)){
                文件DIR = getFilesDir();
                档案文件=新的文件(目录的data.txt);
                布尔删除= file.delete();
                stringToTxt(页,data.txt中);


            }其他{
                stringToTxt(页,data.txt中);
            }

            如果(fileExistance(data_etag.txt)){
                文件DIR = getFilesDir();
                档案文件=新的文件(目录,etag.txt);
                布尔删除= file.delete();
                stringToTxt(ETAGetag.txt);


            }其他{
                //创建etag_file
                stringToTxt(ETAGdata_etag.txt);
            }

            // Log.i(页,页面);
        }赶上(例外五){
            的ShowDialog(数据库获取失败,无法获取密码数据库,检查您的互联网+
                    连接,并稍后重试。,0);
            Log.i(页,错误);
        }

    }
 

解决方案

如果你做一个HTTP 的头在公共或共享Dropbox的网址的要求,你会得到,除其他事项外,一个 ETAG 头。我不知道这种行为是有保障的,因为我不认为这是记录任何地方,但至少现在 ETAG 头可以用来确定当一个文件已经改变。 (如果 ETAG 不同的是,该文件已经修改。)

修改

在一般使用ETag的时候,最有效的做法是发行与如果无 - 匹配标头中的 GET 要求:其中;老ETAG> 。如果内容没有改变,这将有304回应,但如果含量发生了变化,这将下载新的内容,每一个正常的 GET 请求(和响应将是200)。

I want to check for a version changed/get metadata of a text-file with a shared link on dropbox. I will not be using dropbox api as it makes users use their own accounts. I want them to link to my account and I cannot do that manually since I might change my password later.

so: no auth token, just get metadata from shared link of dropbox so that I can check for version changes and if the version has changed download the contents of the new file.

also: I'm open to other suggestions to make this work as well. Please explain in a little detail your solution.

Updated E-Tag Issue:

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");
        }

    }

解决方案

If you do an HTTP HEAD request against a public or shared Dropbox URL, you'll get, among other things, an etag header. I don't know that this behavior is guaranteed, since I don't think it's documented anywhere, but at least for now the etag header can be used to determine when a file has changed. (If the etag is different, the file has changed.)

EDIT

In general when using ETags, the most efficient thing to do is issue a GET request with a header of If-None-Match: <old etag>. If the content hasn't changed, this will respond with a 304, but if the content has changed, this will download the new content as per a normal GET request (and the response will be 200).

这篇关于从Dropbox的链接获取元数据无需身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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