Latest Updates: C# RSS

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

    Phil Leggetter 10:58 am on July 4, 2010 | 0 Permalink | Reply
    Tags: , C#, ,

    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:

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

    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:

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

    Permalink

    | Leave a comment  »

     
  • How I approach problem solving in code?

    Phil Leggetter 6:44 pm on October 23, 2009 | 5 Permalink | Reply
    Tags: , Agile, C#, Coding,

    Recently I was posed the following question:

    Write a piece of code that prints all odd integer numbers between 1 and 99

    This really isn’t a difficult question but it still requires some thought. When I’m posed with any question I like to break things down into their constituent parts.

    Here’s the process I went through:

    Okay, so I’ll define two variables for a start and end value and there’s going to have to be a loop.

    int startValue = 1;
    int endValue = 99;
    for(int i = startValue;
         i <= endValue;
         i++)
    {
       // work out if "i" is an odd number
    }

    Now, for the odd number detection. And… after a few umms and errrs … I’m going to have to mod 2 (%2) the current value of i to work out if the value is odd. More … umms and errs. Okay, I’ve finally worked out that if something mod 2 is not equal to 0 it’s clearly an odd number. This took me longer than it should have but never mind. Once I’ve detected if i is an odd number I’ll then put the odd number into a list for use later.

    int startValue = 1;
    int endValue = 99;
    IList<int> oddValues = new List&lt;int&gt;();
    for(int i = startValue;
         i &lt;= endValue;
         i++)
    {
       if(i%2 != 0)
       {
          oddValues.Add(i);
       }
    }
    

    Those of you that are good at these little puzzles, or just think this is way too easy, might already be screaming at me about one of the following:

    • Why are you using a IList, why don’t you just print the value?
    • Odd numbers are always 2 apart so why aren’t you just increment i by 2 using i+=2?

    (More …)

     
  • How to make a cross domain web request with SilverLight 2

    Phil Leggetter 12:16 pm on October 24, 2008 | 0 Permalink | Reply
    Tags: .NET, , C#, crossdomain, , ,

    To make a cross domain web request with SilverLight 2 really isn’t that tough. I did have some problems with RC0 but I have no idea why. I just tried writing a little app to do this and it worked straight away.
    (More …)

     
  • C# - Get Windows Temporary Directory

    Phil Leggetter 10:25 am on February 2, 2007 | 0 Permalink | Reply
    Tags: , C#, ,

    Once you know it you’ll probably never forget it…but wait, this is the second time I’ve googled for this. I’d best post how you “get the Windows temporary directory using C#“!

    string tempPath =
        System.IO.Path.GetTempPath();
     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel