Compressing HTTP output with an HttpModule (VB) : GZipStream « File Directory « ASP.NET Tutorial






Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.IO.Compression

Public Class Class1
    Implements IHttpModule

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        Throw New Exception("The method or operation is not implemented.")
    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) _
       Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf context_BeginRequest
    End Sub

    Public Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim app As HttpApplication = CType(sender, HttpApplication)

        Dim encodings As String = app.Request.Headers.Get("Accept-Encoding")

        If (encodings = Nothing) Then
            Return
        End If

        Dim s As Stream = app.Response.Filter

        encodings = encodings.ToLower()

        If (encodings.Contains("gzip")) Then
            app.Response.Filter = New GZipStream(s, CompressionMode.Compress)
            app.Response.AppendHeader("Content-Encoding", "gzip")
            app.Context.Trace.Warn("GZIP Compression on")
        Else
            app.Response.Filter = _
                          New DeflateStream(s, CompressionMode.Compress)
            app.Response.AppendHeader("Content-Encoding", "deflate")
            app.Context.Trace.Warn("Deflate Compression on")
        End If
    End Sub
End Class








10.15.GZipStream
10.15.1.Compressing a file using GZipStream (C#)
10.15.2.Compressing a file using GZipStream (VB)
10.15.3.Compressing HTTP output with an HttpModule (C#)
10.15.4.Compressing HTTP output with an HttpModule (VB)