简体   繁体   English

使用apache commons编码消息

[英]encoding a message using apache commons

I am trying to do a base 64 encoding of some message and then URL encode the message and pass the whole encoded content as a parameter/value to a querystring. 我正在尝试对某些消息进行base 64编码,然后对消息进行URL编码,然后将整个编码后的内容作为参数/值传递给查询字符串。 http://www.xxxxxx.com/xxxxx?query=base64urlencodedmessage http://www.xxxxxx.com/xxxxx?query=base64urlencoded消息

So i want to use base64 encoding and then URL encoding. 所以我想使用base64编码,然后再使用URL编码。 I see apache provides good library for it. 我看到apache为此提供了很好的库。

So, in the below apache methods: 因此,在以下apache方法中:

1) how to see which method i want to use 1), 2) and 3) 2) In method 1 below, encoding with a specific char set means encoding with only selected char set? 1)如何查看我要使用哪种方法1),2)和3)2)在下面的方法1中,使用特定字符集进行编码意味着仅使用所选字符集进行编码? what is the difference between 1 and 3 ? 1和3有什么区别? Which is more secure??? 哪个更安全??? 2) I am using base64 first, so does it mean that i will be getting only byte array as output and so I should use only method 2? 2)我首先使用base64,这是否意味着我将仅获得字节数组作为输出,因此我应仅使用方法2?

URLCodec url=new URLCodec(); URLCodec url =新的URLCodec();

1) url.encode(str, charset); 1)url.encode(str,charset);

2) url.encode(bytes); 2)url.encode(bytes);

3) url.encode(str); 3)url.encode(str);

it should be clear for this test 这个测试应该很清楚

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

output 输出

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

BTW you can do the same with standard java.net.URLEncoder / java.net.URLDecoder and javax.bind.DatatypeConverted classes 顺便说一句,您可以对标准java.net.URLEncoder / java.net.URLDecoder和javax.bind.DatatypeConverted类进行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM