1

Given this piece of code:

<%
    Response.Write Server.URLEncode("a doc file.asp")
%>

It output this for a while (like Javascript call encodeURI):

a%20doc%20file.asp

Now, for unknow reason, I get:

a+doc+file%2Easp

I'm not sure of what I touched to make this happen (maybe the file content encoding ANSI/UTF-8). Why did this happen and how can I get the first behavior of Server.URLEncode, ie using a percent encoding?

Amessihel
  • 5,891
  • 3
  • 16
  • 40

1 Answers1

5

Classic ASP hasn't been updated in nearly 20 years, so Server.URLEncode still uses the RFC-1866 standard, which specifies spaces be encoded as + symbols (which is a hangover from an old application/x-www-form-urlencoded media type), you must be mistaken in thinking it was encoding spaces as %20 at some point, not unless there's an IIS setting you can change that I'm unaware of.

More modern languages use the RFC-3986 standard for encoding URLs, which is why Javascript's encodeURI function returns spaces encoded as %20.

Both + and %20 should be treated exactly the same when decoded by any browser thanks to RFC backwards compatibility, but it's generally considered best to use %20 when encoding spaces in a URL as it's the more modern standard now, and some decoding functions (such as Javascript's decodeURIComponent) won't recognise + symbols as spaces and will fail to properly decode URLs that use them over %20.

You can always use a custom function to encode spaces as %20:

function URL_encode(ByVal url)
    
    url = Server.URLEncode(url)
    url = replace(url,"+","%20")
    
    URL_encode = url
    
end function
Community
  • 1
  • 1
Adam
  • 836
  • 2
  • 8
  • 13
  • 2
    I think it was indeed a mistake. Thank you for your answer. – Amessihel Jun 18 '19 at 15:21
  • I think I had an issue with a .NET component finally. I used `Web.HttpUtility.urlencode` instead of `Uri.EscapeUriString`, which is [recommended](https://stackoverflow.com/questions/1517586) for this case. – Amessihel Jun 18 '19 at 16:01