Monday, February 25, 2008

Cool CG Animated Short Film

A CG 3D animated short film, apparently created by just one person. Amazing level of detail, and raised a few smiles as well. Looks very polished and professional.

Friday, February 22, 2008

A Brief Introduction to REST

If you're looking for information on REpresentational State Transfer, then check out A Brief Introduction to REST at InfoQ. I particularly like:

The next principle we’re going to look at has a formal description that is a little intimidating: “Hypermedia as the engine of application state”, sometimes abbreviated as HATEOAS. (Seriously — I’m not making this up.)

Thursday, February 21, 2008

Assigning Basic Authorization HTTP Header to HttpWebRequest

If you're making a call to a HTTP resource that requires a Basic Authorization HTTP header, you can use the following code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url-here");
request.Credentials = new NetworkCredential("username", "password");

Then post the request in the usual way.

The confusion comes because on the first call the HTTP header will not be present on the request. The framework is relying on the first call receiving a 401 response, with a WWW-Authenticate header present, giving a Basic realm=<realm-name> value. A second call will then be made with the correct headers in place.

If you are just consuming the resource, chances are the server will respond in a way that allows this behaviour. However, if it doesn't respond with a 401, or that 401 response doesn't contain the WWW-Authenticate header (or you just don't want to make 2 calls), then you have to manually add the Authorization header to the request:

byte[] authBytes = Encoding.UTF8.GetBytes("user:password".ToCharArray());
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

Wednesday, February 20, 2008

Writing XML with UTF-8 Encoding using XmlTextWriter and StringWriter

If you want to use XmlTextWriter to write XML into a StringBuilder you can create the XmlTextWriter like this:

StringBuilder builder = new StringBuilder();
XmlWriter writer = new XmlTextWriter(new StringWriter(builder));

But this generates a declaration on the resulting XML with the encoding of UTF-16 (the encoding of a .Net String). There doesn't seem to be a straightforward way of making this declaration UTF-8 in this set up.

You can, of course, use a MemoryStream instead of a StringWriter, and then use Encoding.UTF8.GetString(...) to convert the bytes to a string, but doing this made the resulting string have non-printable characters in it, which we don't want.

The solution is to subclass StringWriter and override the Encoding property. Sounds a bit overkill for a solution, but it works very well. Just create the following class (based on Jon Skeet's class):

public class StringWriterWithEncoding : StringWriter
Encoding encoding;

public StringWriterWithEncoding (StringBuilder builder, Encoding encoding)
this.encoding = encoding;

public override Encoding Encoding
get { return encoding; }

Then use StringWriterWithEncoding instead of StringWriter in your XmlTextWriter.

This operation requires IIS integrated pipeline mode

If you're getting an error message

This operation requires IIS integrated pipeline mode

when trying to add headers to a HttpResponse object in an ASP.Net web application, chances are you're trying something like this:

response.Headers["myHeader"] = "someValue";


response.Headers.Add("myHeader", "someValue");

A quick Google reveals mentions of bugs in the 3.5 framework, but I'm using .Net 2. Nevertheless you can work around this problem by adding your header this way instead:

response.AddHeader("myHeader", "someValue");

And your code should now add the header successfully.

Monday, February 11, 2008

50 Open Source Alternatives to Proprietary Programs

The Top 50 Proprietary Programs that Drive You Crazy — and Their Open Source Alternatives

Decent list of open source software for pretty much anything you want to do on your computer.

Tuesday, February 05, 2008

Custom ThreadPool

Last year in a post from the last day at Tech Ed I mentioned a talk about building Scalable ASP.NET Web Applications, in which a warning was given about using the normal .Net ThreadPool in web apps. A suggestion was made to instead use a custom thread pool so that ASP.NET's worker threads weren't all used up.

In that talk it was said there was code for such a class on the Wintellect website, but I remember looking at the time and couldn't find anything.

I've since found Mike Woodring's .NET Sample Page, on which there is a custom thread pool inplementation available for download.

I've not needed to use it yet, so can't comment on its usage, but I'll give it a go when I need control over the ThreadPool.