Reputation:
On start, my application makes a simple GET web-request to a webserver. Depending on the response from the server, the application may need to send another request to the same web server, which response has to contain a block of html code. The question is: How do I compress / minify the response (some html code) to reduce the bandwidth, without needing any 3-rd party libraries for my C# App to decompress the recieved data. I have tought only about base64 encoding (which is not compressing the data), are there any more productive ways?
Upvotes: 2
Views: 282
Reputation: 31202
You can ask the server to give you compressed data if it supports it :
var req = (HttpWebRequest)WebRequest.Create("http://your.server/url");
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
var webResponse = (HttpWebResponse)req.GetResponse();
var responseStream = webResponse.GetResponseStream();
if (webResponse.ContentEncoding.Contains("gzip", StringComparison.OrdinalIgnoreCase))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
else if (WebResponse.ContentEncoding.ToLower().Contains("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
var reader = new StreamReader(responseStream, Encoding.Default);
string html = reader.ReadToEnd();
webResponse.Close();
responseStream.Close();
Full details here : http://www.west-wind.com/weblog/posts/2007/Jun/29/HttpWebRequest-and-GZip-Http-Responses
Upvotes: 0
Reputation: 101614
Well, natively you have GZip compression in .NET. Besides that, you can use other libraries to do the compression. Then, there's always making up your own compression scheme.
Upvotes: 1