• You are here: 
  • Home
  • HttpUtility.UrlEncode Does not encode spaces properly

HttpUtility.UrlEncode Does not encode spaces properly

Posted on January 14th, 2005

I realized a while back that HttpUtility.UrlEncode does not properly encode spaces for URLs.  It encodes a space as a “+“ which most browsers do not interpret as a space for the obvious reason that + is actually a valid character for a filename.  So to work around the bug we wrote the following:

public static string URLEncode(string str)
{
       return System.Web.HttpUtility.UrlEncode( str ).Replace(”+”,”%20″);
}

It’s interesting to note that HttpUtility.UrlPathEncode does properly encode the space but fails to encode other critical charcters like & and ?.

Filed under .NET |

2 Responses to “HttpUtility.UrlEncode Does not encode spaces properly”

  1. Anonymous Says:
    February 16th, 2005 at 3:15 pm

    In my small experiments I have seen the following;

    string e = “

What I'm Doing...

Posting tweet...

Powered by Twitter Tools.

Tags

Archives

Meta