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

查看:22
本文介绍了为什么要进行 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.

但是空格、/、% 都是 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>

而君特的会是

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

我的,大多数浏览器都可以处理.君特的,可能不是.

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

编码,这些会变成

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

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

哪些是有效的 URL.

which are valid URLs.

(请原谅,大多数设计良好的系统不会在这样的参数中传递用户名.这只是澄清概念的示例.)

(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天全站免登陆