Entries in XNA (46)

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];

    try
    {
        // prepare the web page we will be asking for
        HttpWebRequest request = 
            (HttpWebRequest)WebRequest.Create(url);

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

        // execute the request
        HttpWebResponse response =
            (HttpWebResponse)request.GetResponse();

        // we will read data via the response stream
        Stream resStream;
        Stream inputStream =
            response.GetResponseStream();

        // Might have a compressed stream coming back

        switch (response.ContentEncoding)
        {
            case "gzip":
                resStream =
                   new GZipStream(inputStream, 
                          CompressionMode.Decompress);
                break;
            case "":
                resStream = inputStream;
                break;
            default :
                debugOutput.PutMessageLine(
                    "Invalid content encoding: " +
                    response.ContentEncoding + " in: "
                    + url);
                return null;
        }

        string tempString = null;
        int count = 0;

        do
        {
            // 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 = 
                Encoding.ASCII.GetString(buf,0,count);

              // continue building the string
              sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?

        return sb;
    }
    catch (System.Net.WebException w)
    {
        debugOutput.PutMessageLine(w.Message);
        return null;
    }
    catch (System.Net.ProtocolViolationException p)
    {
        debugOutput.PutMessageLine(p.Message);
        return null;
    }
    catch (Exception e)
    {
        debugOutput.PutMessageLine(e.Message);
        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.

Posted on Monday, June 16, 2008 at 10:20AM by Registered CommenterRob in , | Comments2 Comments

Cheesy Music

I'm doing a session at Dev Days next week in their Geek Night slot. It is on XNA, and so I thought I'd have a play with "Hot Salad Death with Cheese" and get it running on the Zune.

Took 30 minutes.

Took another 20 to make the game play a random music track and display a spectrum analyzer as the music plays. Great stuff. I'm really looking forward to the talk now..

2503158864

Hot salad death with music and cheese.

Posted on Sunday, May 18, 2008 at 09:00PM by Registered CommenterRob in | Comments4 Comments

Buy My Book (please)

I was overjoyed to find last week that Waterstones bookshop in Hull had a copy of my book in stock.

I was less overjoyed to discover this week that the copy is still there. Now I'm fretting as to why nobody has bought it. Of course, they might have sold hundreds and kept refilling the shelf.

But I doubt it.

I got a royalty cheque yesterday and so there must be some copies selling somewhere, but I'd love for the one in my home town (so to speak) to sell as well.

Posted on Saturday, May 17, 2008 at 07:56PM by Registered CommenterRob in | Comments5 Comments

Blue Zune

2489894512
All that work. Just for this!

I've got my Zune working on my newly fettled PC. To celebrate I've made the screen go all blue (actually this is the default project). Time to get some cheese in there methinks.

Posted on Tuesday, May 13, 2008 at 04:17PM by Registered CommenterRob in , | Comments3 Comments

Rob in Micro Mart

You know you've made it big when you are in Micro Mart. At the end of yesterday's lecture I was shown a picture of my ugly mug staring out of page 94 of issue 997 of Micro Mart magazine. It was part of an article about Social Gaming, and I had been quoted saying sensible things about XNA (I must have been caught on one of my "sensible days" - which are getting fewer and further between these days).

Of course I shot over to the Students Union and bought a copy of the magazine (incidentally, and I've no idea why this should be the case, but our Students Union shop has an absolutely fantastic range of magazines. There are titles there that I've had bother finding in other places. Kudos.)

Anyhoo, I like the magazine. And not just because it has me in it. For a weekly magazine it has some very good editorial content and it made a very good lunchtime read. Of course the downside is that it also has lots of adverts for stuff, and so it might have proved a rather expensive purchase.....

Posted on Thursday, April 3, 2008 at 07:18PM by Registered CommenterRob in , | Comments5 Comments
Page | 1 | 2 | 3 | 4 | 5 | Next 5 Entries