Basic Authentication against the Superfeedr HTTP PubSubHubbub API using a .NET HttpWebRequest

04 Jul 2010

It would appear that setting the Credentials property of a HttpWebRequest still leads to authentication failure against the Superfeedr HTTP PubSubHubbub API. This might be because the way the HttpWebRequest works is that it first waits to be challenged for credentials via a 401 (not authorized) and then replies with the authentication (more info include PreAuthenticate here).

So, this doesn't work:
[code lang="csharp"]
string username = "username";
string password = "password";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url-here");
request.Credentials = new NetworkCredential(username, password);
[/code]

To work around this you need to force the authentication to be sent. I found the details of how to do this in a blog post by Ian Dykes here:

http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html

But for fullness here's the code:

[code lang="csharp"]
string username = "username";
string password = "password";
byte[] authBytes = Encoding.UTF8.GetBytes(username + ":" + password.ToCharArray());
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);
[/code]

Permalink

| Leave a comment  »