Loading Web Pages

I don't usually put up techie pages with code in them, but today I present something that I think might be useful and I've not found anywhere else. It is part of my RSS reader for the XNA display program, and it solves a couple of problems that you might hit. My feed reader needs to read feeds that are on a password protected site, and it also needs to read feeds that are compressed. This method does both:

private StringBuilder readWebPage(
    string url,
    string username,
    string password,
    string domain)
    // used to build entire input
    StringBuilder sb = new StringBuilder();

    // used on each read operation
    byte[] buf = new byte[8192];

        // prepare the web page we will be asking for
        HttpWebRequest request = 

        if (username.Length > 0)
            request.Proxy = null;
            NetworkCredential credential = 
               new NetworkCredential(username,
            request.Credentials = credential;

        // execute the request
        HttpWebResponse response =

        // we will read data via the response stream
        Stream resStream;
        Stream inputStream =

        // Might have a compressed stream coming back

        switch (response.ContentEncoding)
            case "gzip":
                resStream =
                   new GZipStream(inputStream, 
            case "":
                resStream = inputStream;
            default :
                    "Invalid content encoding: " +
                    response.ContentEncoding + " in: "
                    + url);
                return null;

        string tempString = null;
        int count = 0;

            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if (count != 0)
              // translate from bytes to ASCII text
              tempString = 

              // continue building the string
        while (count > 0); // any more data to read?

        return sb;
    catch (System.Net.WebException w)
        return null;
    catch (System.Net.ProtocolViolationException p)
        return null;
    catch (Exception e)
        return null;

This is not very elegant code, but it does work. I've pulled it straight out of the program and so there are a few things you need to know. I have a class called debugOutput which I use to send messages to the user. You either need to create one of your own, or remove those lines. If you need to use a username and password you need to add those to the call, otherwise you can leave those fields as empty strings.