前言
- java中的urldecoder和urlencoder是用于对url进行编码和解码的类。
- url编码是将url中的特殊字符转换成特定的格式,以便于在url中传递参数。urldecoder类提供了decode()方法,可以将经过url编码的字符串解码成原始字符串。
- urlencoder类提供了encode()方法,可以将字符串编码成url编码的格式。
规则
urlencoder
在对字符串进行编码时,下列规则适用:
- 字母数字字符“
a
”通过“z
”、“a
”通过“z
”和“0
”通过“9
”保持不变。 - 特殊字符
.
”、“-
”、“*
”、和“_
”保持不变。 - 空间性” ”转变为一个加号“
+
”。
所有其他字符都是不安全的,并首先被转换成一个或多个字节,使用一些编码方案。然后每个字节表示的字符串“% xy”,xy是两位十六进制表示的字节。推荐使用utf-8编码方案。然而,对于兼容性的原因,如果没有指定的编码,则使用该平台的默认编码。
例如使用utf-8编码格式的字符串的字符串ü@ foo bar”会转化为“+字符串+ % % % c3前40foo吧”因为在utf-8字符编码为字节ü两c3(hex)和bc(hex),和字符编码为一个@字节40(hex)。
urldecoder
转换过程是由urlencoder班采用反向。这是假设在编码的字符串的所有字符都是下列之一:“a
”通过“z
”、“a
”通过“z
”、“0
”通过“9
”、和“-
”、“_
”、“.
”、和“*
”。字符“%
”是允许的但被解释为一种特殊的序列开始逃跑。
下面的规则应用在转换中:
- 字母数字字符“
a
”通过“z
”、“a
”通过“z
”和“0
”通过“9
”保持不变。 - 特殊字符
.
”、“-
”、“*
”、和“_
”保持不变。 - 加号“
+
”转换为空格字符“ ”。 - 一个序列的形式“xy”将被视为代表一个字节xy是两位十六进制表示的8位。然后,包含一个或多个这些字节序列连续将由字符替换所有子串(s)的编码会导致那些连续字节。用于对这些字符进行解码的编码方案可以被指定,或者如果未指定,则该平台的默认编码将被使用。
有两种可能的方法,该解码器可以处理非法字符串。它可以把非法字符单独或可能引发illegalargumentexception。该方法的解码器需要的执行。
方法
urlencoder
modifier and type | method and description |
---|---|
static string | encode(string s)过时的。 由此产生的字符串可能会有所不同,这取决于该平台的默认编码。相反,使用编码(字符串,字符串)方法来指定编码。 |
static string | encode(string s, string enc) 翻译成 application/x-www-form-urlencoded格式字符串使用一个特定的编码方案。 |
urldecoder
modifier and type | method and description |
---|---|
static string | decode(string s)过时的。 由此产生的字符串可能会有所不同,这取决于该平台的默认编码。相反,使用解码(字符串,字符串)方法来指定编码。 |
static string | decode(string s, string enc) 解码使用特定的编码方案 application/x-www-form-urlencoded字符串。 |
使用
以下是urldecoder和urlencoder的使用示例:
url编码示例:
string url = "https://www.example.com/?name=john doe"; string encodedurl = urlencoder.encode(url, "utf-8"); system.out.println(encodedurl);
输出结果:
https%3a%2f%2fwww.example.com%2f%3fname%3djohn+doe
url解码示例:
string encodedurl = "https%3a%2f%2fwww.example.com%2f%3fname%3djohn+doe"; string decodedurl = urldecoder.decode(encodedurl, "utf-8"); system.out.println(decodedurl);
输出结果:
https://www.example.com/?name=john doe
在使用urldecoder和urlencoder时,需要传入charset参数指定字符编码。
常用的字符编码包括"utf-8"、"iso-8859-1"等。编码和解码时需要使用相同的字符编码,否则可能会出现乱码问题。
案例
在实际使用中,urldecoder和urlencoder主要用于处理url中的特殊字符和参数的编码。
以下是一些使用urldecoder和urlencoder的常见场景和示例:
1.编码url参数
在向url中添加参数时,需要对参数进行编码,以确保特殊字符不会破坏url的结构。例如:
string param1 = "john doe"; string param2 = "hello, world!"; string encodedparam1 = urlencoder.encode(param1, "utf-8"); string encodedparam2 = urlencoder.encode(param2, "utf-8"); string url = "https://www.example.com/?name=" + encodedparam1 + "&message=" + encodedparam2; system.out.println(url);
输出结果:
https://www.example.com/?name=john+doe&message=hello%2c+world%21
2.解码url参数
在接收到url参数后,需要对参数进行解码,以获取原始的参数值。例如:
string encodedparam = "hello%2c+world%21"; string decodedparam = urldecoder.decode(encodedparam, "utf-8"); system.out.println(decodedparam);
输出结果:
hello, world!
3.解析url
当需要解析url中的各个部分时,urldecoder和urlencoder也可以派上用场。
例如,解析url中的查询参数:
string url = "https://www.example.com/?name=john+doe&age=30"; url parsedurl = new url(url); string query = parsedurl.getquery(); // 获取查询参数部分 string[] params = query.split("&"); // 拆分参数 for (string param : params) { string[] keyvalue = param.split("="); // 拆分键值对 string key = urldecoder.decode(keyvalue[0], "utf-8"); string value = urldecoder.decode(keyvalue[1], "utf-8"); system.out.println("key: " + key + ", value: " + value); }
输出结果:
key: name, value: john doe
key: age, value: 30
注意:
在真实的应用中,需要注意处理异常、选择合适的字符编码和适当地进行参数校验和处理。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论