Entries in development (47)
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.
Blue Zune
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.
Students at TechEd 2008
One of our students came to see me this morning. He is hoping to go to TechEd 2008 in Orlando and wanted me to write a supporting letter for his visa application.
You bet I will. I'm actually going to TechEd 2008 myself (I'm giving a session on the .NET Micro Framework) and I know what a great experience it is. Particularly if you are a student.
I've advised him to prepare carefully before he goes to make sure that he gets as much as possible out of the trip. He should be able to get his session schedule sorted out in advance of the conference, and he really should make use of the Hands On Labs to play with the latest toys. There are also discount Microsoft Certified Professional tests that can be taken on site, but for me the most wonderful feature of the event is the "Ask the Experts" bit.
It continues to amaze me that Microsoft puts its absolute top dogs out there for the public to harangue. Perhaps it is because they are too cheap to hire "booth babes", but I like to think that it is because the really want their developers to meet up with the people that use their stuff.
When you go and talk to someone on the XNA, or Micro Framework or Sharepoint or whatever stand at "Ask the Experts" you are actually talking to the product managers and developers that make the product. They aren't just customer support people, they are the authors themselves. Most delegates at the conference just use this opportunity to see what swag is being given away at each booth. But a few have cottoned on to this part of the conference and take the opportunity to get deep answers to hard problems.
I've told our student to make sure that he goes around the experts and has a chat with them, from my experience they love talking about the stuff they make and why they think it is the best in the business. I really hope he makes it out there, I just wish more students could get to go.
XNA now available for Zune
Alfred reports that you can now get the first CTP version of XNA 3.0. This is the one that will make it possible to create and deploy games on Microsoft Zune devices. Wonderful stuff. If you want to write games for mobile devices you should take a look. All we want now, Mr. Microsoft, is a UK release of the Zune. Please.
Very Silly Simon
I've had a rush of creativity. No, really.
This is a screenshot of the Simple Simon Project at Very Silly Games. It will eventually be a pattern matching game remarkably like another game named after a pieman.
I've only written the first bit for now, with some questions that you can use to brush up your programming smarts. Study the code and answer them and we will see about moving onto the next phase of the development.
I've just remembered. We never get to know the name of the pieman. Just the person who met him.


