.Net URL Encoding: should a space be '+' (plus symbol) or '%20'?

Posted on 2009-04-29
Last Modified: 2013-11-12
I'm using .Net libraries to encode strings for placement into URLs.  I noticed that httputility.urlencode chooses to encode the space character with '+' (a plus symbol) not '%20' (the hex value).

This is fine with me.  Except that my WCF REST service does not appear to be happy with the '+' encoding.  If I <string>.Replace the '+''s for '%20''s, everything works out just fine.  I suppose this is functional.  But, I feel like I'm missing something about how this library is supposed to work.  

I must confess to also being a bit new to WCF.  So, on the service side, I'm not clear where the decoding is happening.  But, by the time the WCF infrastructure passes the value to my code (in debug mode), it is decoded.

To make matters more confusing, if I put '+''s for spaces in the browser address window when debugging my WCF REST service, it appears to handle it fine.  It is only when the service is running in IIS that the problem occurs.  This string, when appended to the end of a URL, causes failure with IIS, but runs OK in debug mode: "Las+Vegas%2c+NV".  This string, no matter how I send it to the service, works fine: "Las%20Vegas%2c%20NV".

For those that have read this far and are wondering about the client application that is encoding the URL, I'm doing it from a SilverLight 2.0 application.

Lastly, when I preview this question and code, I've noticed that the plus symbol ('+') appears as a space (' ').  I guess Expert Exchange is urldecoding this.  Oi.  At any rate, hopefully my comments are clear enough that you can see where I mean plus symbol and where I mean space.  
// unhappy 

           //string a = HttpUtility.UrlEncode( txtInput.Text);


           // happy

           string a = HttpUtility.UrlEncode( txtInput.Text).Replace("+","%20");

            Uri u = new Uri(@"<Root of my WCF REST service>" + a);

            WebClient rest = new WebClient();

            rest.DownloadStringCompleted += new DownloadStringCompletedEventHandler(rest_DownloadStringCompleted);


Open in new window

Question by:dannykrouk
    LVL 8

    Accepted Solution

    use System.Uri.EscapeDataString instead of HttpUtility.UrlEncode to encode.
    use HttpUtility.UrlDecode to decode.
    for more information, check this  

    Author Closing Comment

    Cha-ching!  Thanks.  That helps quite a bit.  The different behavior of the service when running in the debugger vs. in IIS was a secondary issue.  The method (and link) to which you refer are exactly what I needed.  Many thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now