android - Cloudant/CouchDB key /通行证权限不起作用

标签 android https urlconnection cloudant

我在使用具有读取权限(或所有权限)的 key /通行证从 Cloudant.com 获取信息时遇到问题。无论以何种方式设置此用户,我都会收到 500 错误。然而,目前一切工作正常,但我让自己容易受到黑客攻击,因为我必须打开我的数据库才能被每个人读取。我们都知道这可能是一个问题。我想知道是否有人对为什么会发生这个问题有任何见解。顺便说一句,我在 Android 和 iOS 应用程序中都尝试过。当前的问题使用 Android 应用程序中的示例。

这是我的单例乌托邦的标准:

private static String _remote_db_protocal = "https";
private static String _remote_db_key = "mykey";
private static String _remote_db_pass = "mypass";
private static String _remote_db_account = "myaccount";
private static String _remote_db_dbname = "mydbname";

public static String REMOTE_JSON_DB_URL = _remote_db_protocal+"://"+
        _remote_db_key+":"+_remote_db_pass+"@"+
        _remote_db_account+".cloudant.com/"+_remote_db_dbname;

以下是我为获取 URL 字符串响应而发送的信息:

    private static String dbBaseURL = Utopia.REMOTE_JSON_DB_URL;
    .... 
    String dbQueryURL = "/_design/app/_view/toys";
    URL finalURL;
    try {

        finalURL = new URL(dbBaseURL + dbQueryURL);
        Log.i(TAG, "Getting URL: "+finalURL.toString());

        response = WebStuff.getURLStringResponse(finalURL);
   ....

最后,这是我的 getURLStringResponse() 函数:

/*
 *  Get information back from a URL
 */
public static String getURLStringResponse(URL url)
{
    String response = "";

    try {
        URLConnection conn = url.openConnection();
        BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());

        byte[] contentBytes = new byte[1024];
        int bytesRead = 0;
        StringBuffer responseBuffer = new StringBuffer();

        while ((bytesRead = bin.read(contentBytes)) != -1)
        {
            response = new String(contentBytes, 0, bytesRead);
            responseBuffer.append(response);
        }

        return responseBuffer.toString();

    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, "getURLStringResponse Error as Follows");
        Log.e(TAG, e.toString());
    }

    return response;
}

综上所述,希望有人能解答这个问题。我还必须注意,我要从日志中某处吐出的查询中获取确切的 URL,或者只是从 REMOTE_JSON_DB_URL 变量和我正在查找的值重新创建它,然后将其传递到浏览器中,一切正常(是的,正在注销我的帐户)。

编辑: 更正,当我将其粘贴到浏览器中时,它会要求我提供登录凭据,并使用 key /密码使其工作。一位 Cloudant 开发人员要求我向 StackOverflow 提出这个问题,因为他们不确定发生了什么(他们还拥有我的整个应用程序以及 key /通行凭证等)。

最佳答案

好吧,我好像已经明白了。昨天得到威尔的答案后,我走了几条不同的路,但可惜的是,大多数都没有让我接近。其中一些会将错误代码从 500 更改为 401,但这些也改变了 BufferedInputStream 的预期用途。无论如何,如果您想使用带有 URLConnection 的 BufferedInputStream,并包含对 Cloudant 等内容或任何情况的身份验证,则需要使用 setRequestProperty。现在,话虽如此,我尝试了几种不同的方法,添加用户和密码,并在建立连接并将 url 传递到函数后进行 Base64 切换,但根本不起作用。因此,如果您能够像我最初那样通过 URL 传递 key /通行证,那么以下内容将非常适合您......理论上(无论如何,这对我有用)。

        if (url.getUserInfo() != null) {
            String basicAuth = "Basic " + new String(Base64.encode(url.getUserInfo().getBytes(), 0));
            conn.setRequestProperty("Authorization", basicAuth);
        }

当然,这里它是在我的函数中实现的,因此您可以获得完整的效果。

public static String getURLStringResponse(URL url)
{
    String response = "";

    try {
        URLConnection conn = url.openConnection();

        // THIS WAS THE MAGIC ENTRY
        if (url.getUserInfo() != null) {
            String basicAuth = "Basic " + new String(Base64.encode(url.getUserInfo().getBytes(), 0));
            conn.setRequestProperty("Authorization", basicAuth);
        }
        // END OF MAGIC CHANGE

        BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());

        byte[] contentBytes = new byte[1024];
        int bytesRead = 0;
        StringBuffer responseBuffer = new StringBuffer();

        while ((bytesRead = bin.read(contentBytes)) != -1)
        {
            response = new String(contentBytes, 0, bytesRead);
            responseBuffer.append(response);
        }

        return responseBuffer.toString();

    } catch (Exception e) {
        // TODO: handle exception
        Log.e(TAG, "getURLStringResponse Error as Follows");
        Log.e(TAG, e.toString());
    }

    return response;
}

我真的希望这对你们中的一些人有所帮助。

关于android - Cloudant/CouchDB key /通行证权限不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18408070/

相关文章:

http - 使用 Http 代理与 https 代理的优缺点?

ios - Charles 代理 SSL 问题

java - 真的有必要使用 url.openConnection() 吗?

android - 在 Lollipop 及以上版本上仅获得一次 sdcard 窗口的权限

java - 如何将Android字符串数组加载到ListView中?

tomcat - 通过连接器为同一个 Tomcat 应用程序提供不同的证书?

java - URLConnection.setDoOutput() 究竟有什么影响?

java - 从 AndroidHttpClient 迁移到 URLConnection

Android GCM 基本实现

android - 当应用程序未运行时,GCM 推送通知不会在某些设备中显示