简体   繁体   中英

Base64 Encode with Custom Character Set

I [think I] need to do a base 64 encoding in C# with a custom character set, but I can't figure out how.

Specifically, I'm trying use the Mixpanel HTTP spec - https://mixpanel.com/docs/api-documentation/http-specification-insert-data - from within a Unity3D project. Mixpanel says that it has a custom "flavor" of Base64 encoding, and indeed, when I encode the same JSON with a web service that lets me specify the character set ( http://www.motobit.com/util/base64-decoder-encoder.asp ), Mixpanel reports success.

(Thus, I know that the failure is in the encoding, not the data being encoded)

The example provided by the page for a URL does not match the example for a JSON structure.

Here is a working example:

var json = "{\"event\": \"game\", \"properties\": {\"ip\": \"123.123.123.123\", \"token\": \"e3bb4100330c35722740fb8c6f5abddc\", \"time\": 1245613885, \"action\": \"play\"}}";

var result = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));

// result == "eyJldmVudCI6ICJnYW1lIiwgInByb3BlcnRpZXMiOiB7ImlwIjogIjEyMy4xMjMuMTIzLjEyMyIsICJ0b2tlbiI6ICJlM2JiNDEwMDMzMGMzNTcyMjc0MGZiOGM2ZjVhYmRkYyIsICJ0aW1lIjogMTI0NTYxMzg4NSwgImFjdGlvbiI6ICJwbGF5In19"

Since it's encoding to Base64, but being used for web, it's custom set is based on that. See: C# Byte[] to Url Friendly String

Edit: Perhaps not... it seems they allow '+' and '/'. Strange.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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