为什么要进行url编码,或者要编码哪些字符 [英] Why url encode, or which characters to encode

查看:100
本文介绍了为什么要进行url编码,或者要编码哪些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.w3schools.com/tags/ref_urlencode.asp


由于URL通常包含ASCII集以外的字符,因此URL有
可转换为有效的ASCII格式。

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

但是,space,/,%都是ASCII字符集的一部分,所以为什么要先编码它们?什么字符应该被编码?

But both space, /, % are part of the ASCII character set, so why encode them in the first place? What characters should be encoded?

推荐答案

如上所述,空格在URL中无效。大多数浏览器自动对它们进行编码。然而......

As noted, spaces are not valid in URLs. Most browsers orlencode them automatically. However...

您链接的页面有一个例子,在试用我一节中使用单词Günter。如果单词Günter作为查询字符串参数发送,则不起作用。因为ü不在标准的ASCII字符集中。

The page you linked to has an example using the word "Günter" in the "Try me" section. If the word "Günter" were sent as a querystring parameter, it wouldn't work. as the "ü" is not in the standard ASCII character set.

当可能存在非ASCII字符时使用它。一个示例可能是使用数据库中的数据创建超链接。假设代码创建一个链接到用户个人资料页面。未编码,我的将是:

It's meant to be used when there are potentially non-ASCII characters. An example might be when using data from a database to create a hyperlink. Suppose the code creates a link to a user profile page. Unencoded, mine would be:

<a href="profile/?username=David Stratton">Your profile</a>

而Günter的将是

while Günter's would be

<a href="profile/?username=Günter">Your profile</a>

大多数浏览器都可以处理。 Günter的,可能不是。

Mine, most browsers could handle. Günter's, probably not.

编码后,这些将成为

Encoded, these would become

<a href="profile/?username=David%20Stratton">Your profile</a>

<a href="profile/?username=G%FCnter">Your profile</a>

这些是有效的网址。

(请原谅这样一个事实,即大多数精心设计的系统不会像这样的参数中传递用户名,这只是一个例子来澄清这个概念。)

(Please forgive the fact that most well-designed systems wouldn't pass a username in a parameter like that. This was just a sample to clarify the concept.)

这篇关于为什么要进行url编码,或者要编码哪些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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