Walter's profileWalter's SpacePhotosBlogListsMore Tools Help

Blog


    June 29

    Gotchas with System.Uri

    System.Uri is a class that has several caveats with usage.  Three methods deserve scrutiny:
    • Uri.ToString()
    • Uri.EscapeUriString()
    • Uri.AbsoluteUri

    The first two functions should be banned from usage because they lead to several unintended behaviors.  Demonstration: take a nice, pretty url like below.  It has double-url-encoding to preserve the rru parameter (highlighted)

    http://mail.live.com/mail/mail.aspx?=&rru=getmsg%3fcurmbox%3d00000000%252d0000%252d0000%252d0000%252d000000000001%26a%3dc7913ec40534cbf9582851359b6128a9404dbea8a6f7faec4d100a9f1499b1c5%26msg%3d9BC88EF5-0A29-4D7A-B0D3-5607DF3635C3%26start%3d0%26len%3d380290%26msgread%3d1&wa=wsignin1.0


    Output of Uri.AbsoluteUri.  This is correct.

    http://mail.live.com/mail/mail.aspx?=&rru=getmsg?curmbox=00000000%252d0000%252d0000%252d0000%252d000000000001&a=c7913ec40534cbf9582851359b6128a9404dbea8a6f7faec4d100a9f1499b1c5&msg=9BC88EF5-0A29-4D7A-B0D3-5607DF3635C3&start=0&len=380290&msgread=1&wa=wsignin1.0


    Output of Uri.ToString().  You'll notice that the double-url-encoding is removed (highlighted).  This breaks the original encoded parameter; rru will no longer be parseable.  You would think Uri.ToString() is safe, but no it isn't .  Windows Live Mail had to take a hotfix because click thrus from Messenger, http://www.live.com, http://my.msn.com, and http://www.msn.com were broken.

    http://mail.live.com/mail/mail.aspx?=&rru=getmsg?curmbox=00000000%252d0000%252d0000%252d0000%252d000000000001&a=c7913ec40534cbf9582851359b6128a9404dbea8a6f7faec4d100a9f1499b1c5&msg=9BC88EF5-0A29-4D7A-B0D3-5607DF3635C3&start=0&len=380290&msgread=1&wa=wsignin1.0


    Output of Uri.EscapeUriString(Uri.ToString()).  This is just completely messed up.  In addition to the url decoding, there is an additional encoding of % into %25 (highlighted)

    http://mail.live.com/mail/mail.aspx?=&rru=getmsg?curmbox=00000000%25252d0000%25252d0000%25252d0000%25252d000000000001&a=c7913ec40534cbf9582851359b6128a9404dbea8a6f7faec4d100a9f1499b1c5&msg=9BC88EF5-0A29-4D7A-B0D3-5607DF3635C3&start=0&len=380290&msgread=1&wa=wsignin1.0

     

    In conclusion, the method you really want to use is Uri.AbsoluteUri.