Server.URLEncode开始用加号("+")代替百分号20(“%20")的空白 [英] Server.URLEncode started to replace blank with plus ("+") instead of percent-20 ("%20")

查看:81
本文介绍了Server.URLEncode开始用加号("+")代替百分号20(“%20")的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这段代码:

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

它会输出一段时间(例如Javascript调用 encodeURI ):

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

a%20doc%20file.asp

现在,由于未知原因,我得到:

Now, for unknow reason, I get:

a+doc+file%2Easp

我不确定要实现此目标的方式(可能是编码ANSI/UTF-8的文件内容).为什么会发生这种情况?如何获得 Server.URLEncode 的第一个行为,即使用百分比编码?

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?

推荐答案

经典ASP已有近20年没有更新,因此 Server.URLEncode 仍使用

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.

更多现代语言使用 RFC-3986 标准对URL进行编码,这就是Javascript的原因 encodeURI 函数返回编码为%20 的空格.

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

+ %20 在被任何浏览器解码时应完全相同,但是通常认为最好使用%20 ,因为它是目前最现代的标准,因此对URL中的空格进行编码时,某些解码功能(例如Javascript的 decodeURIComponent )不会将 + 符号识别为空格,将无法正确解码在%20 上使用它们的URL.

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.

您始终可以使用自定义函数将空格编码为%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

这篇关于Server.URLEncode开始用加号("+")代替百分号20(“%20")的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆