I want to create a mechanism (in C#) where text from a QueryString is displayed on a website.
For example, in C# I might literally do;
public void Page_Load(blah)
{
litSomething.text = Reques.QueryString["msg"];
}
Assume that the message is written in English (allowing UTF8 would be nice), and is no longer than say 1000 characters. I want to compress this text down as much as possible and still be able to place it in a QueryString.
We can predefine as many dictionary terms as we like (well with-in reason?). The server side code will encode and decode the messages.
(Obviously I'll be adding in all the usual XSS protection, HttpUtitlity.HtmlEncode etc type stuff. Also pointers to free dictionary sources would be good!)
Any tips, adivce, source code? This isn't my homework before you ask!
Update
Thanks for the suggestions. I want to make this a GET, so people IM/email URLs. Im thinking along the lines of bit.ly which would also be a cheat in itself. Wanted this to be a generic "short text compression" question though.
You can encode the string as UTF-8 so that you get a byte array, that you can compress. The result is also a byte array, so you can use Base-64 encoding to get it as a string:
private static string Compress(string data) {
using (MemoryStream ms = new MemoryStream()) {
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) {
zip.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
}
Decompressing is just the other way around:
private static string Decompress(string data) {
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data))) {
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true)) {
using (BinaryReader reader = new BinaryReader(zip)) {
return Encoding.UTF8.GetString(reader.ReadBytes(10000));
}
}
}
}
Well, the immediate problems are:
This means that if you can't cope with (say) ~1300 characters in the query string, there's no guarantee that it will always work. (As Marc says, use the body of a POST instead if you possibly can... then you can probably ignore compression in the first place.)
If you're happy with those though, there's nothing particularly different about your situation than any other:
Convert.ToBase64String
(and then replace web-nasty characters)On the other side, apply the same transformation in reverse.
Given that the compression API is stream-based, you could use StreamWriter
to avoid explicitly converting from text to binary first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With