【问题标题】:encoding a message using apache commons使用 apache commons 对消息进行编码
【发布时间】:2013-07-25 11:27:15
【问题描述】:

我正在尝试对某些消息进行 base 64 编码,然后对消息进行 URL 编码,并将整个编码内容作为参数/值传递给查询字符串。 http://www.xxxxxx.com/xxxxx?query=base64urlencodedmessage

所以我想使用 base64 编码,然后是 URL 编码。我看到 apache 为它提供了很好的库。

所以,在下面的 apache 方法中:

1) 如何查看我要使用哪种方法 1)、2) 和 3) 2) 在下面的方法 1 中,使用特定字符集进行编码意味着仅使用选定的字符集进行编码? 1 和 3 和有什么不一样?哪个更安全??? 2)我首先使用base64,这是否意味着我将只获得字节数组作为输出,所以我应该只使用方法2?

URLCodec url=new URLCodec();

1) url.encode(str, charset);

2) url.encode(字节);

3) url.encode(str);

【问题讨论】:

    标签: java javascript apache encoding apache2


    【解决方案1】:

    这个测试应该很清楚

        String s1 = "test";
        System.out.println(s1);
        Base64 base64 = new Base64();
        String s2 = base64.encodeAsString(s1.getBytes());
        System.out.println(s2);
        URLCodec url = new URLCodec();
        String s3 = url.encode(s2);
        System.out.println(s3);
        s2 = url.decode(s3);
        System.out.println(s2);
        s1 = new String(base64.decode(s2));
        System.out.println(s1);
    

    输出

    test
    dGVzdA==
    dGVzdA%3D%3D
    dGVzdA==
    test
    

    顺便说一句,您可以对标准 java.net.URLEncoder / java.net.URLDecoder 和 javax.bind.DatatypeConverted 类做同样的事情

    【讨论】:

    • 谢谢Evgeniy ...帮助...但是我能知道为url编码指定字符集的优点/缺点吗???
    猜你喜欢
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多