您的位置:首页 > 其它

URL编码

2017-01-13 00:00 148 查看
摘要: URL编码

URL编码

URL编码(URL encoding), 是特定上下文的统一资源定位符 (URL)的编码机制. 实际上也适用于统一资源标志符(URI)的编码。也用于为"application/x-www-form-urlencoded" MIME准备数据, 因为它用于通过HTTP的请求操作(request)提交HTML表单数据。

如果一个保留字符在特定上下文中具有特殊含义(称作"reserved purpose") , 且URI中必须使用该字符用于其它目的, 那么该字符必须百分号编码. 百分号编码一个保留字符,首先需要把该字符的ASCII的值表示为两个16进制的数字,然后在其前面放置转义字符("%"),置入URI中的相应位置。(对于非ASCII字符, 需要转换为UTF-8字节序, 然后每个字节按照上述方式表示.)

保留字符的URL编码

!
#
$
&
'
(
)
*
+
,
/
:
;
=
?
@
[
]
%21
%23
%24
%26
%27
%28
%29
%2A
%2B
%2C
%2F
%3A
%3B
%3D
%3F
40%
%5B
%5D
application/x-www-form-urlencoded类型

当HTML表单中的数据被提交时,表单的域名与值被编码并通过HTTP的GET或者POST方法甚至更古远的email把请求发送给服务器。这里的编码方法采用了一个非常早期的通用的URI百分号编码方法,并且有很多小的修改如新行规范化以及把空格符的编码"%20"替换为"+" . 按这套方法编码的数据的MIME类型是application/x-www-form-urlencoded, 当前仍用于(虽然非常过时了)HTML与XForms规范中. 此外,CGI规范包括了web服务器如何解码这类数据、利用这类数据的内容。

如果发送的是HTTP GET请求, application/x-www-form-urlencoded数据包含在所请求URI的查询成分中. 如果发送的是HTTP POST请求或通过email, 数据被放置在消息体中,媒体类型的名字被包含在消息的Content-Type头内部。

java提供了URLEncoder类和URLDecoder类。

public class URLDecoder extends Object

public class URLEncoder extends Object

一、URLEncoder

import java.net.URLEncoder;

import java.net.URLDecoder;

import java.io.UnsupportedEncodingException;

public class EncoderTest {

public static void main(String[] args) {

try {

System.out.println(URLEncoder.encode("This string has spaces","UTF-8"));

System.out.println(URLEncoder.encode("This*string*has*asterisks","UTF-8"));

System.out.println(URLEncoder.encode("This%string%has%percent%signs", "UTF-8"));

System.out.println(URLEncoder.encode("This+string+has+pluses","UTF-8"));

System.out.println(URLEncoder.encode("This/string/has/slashes","UTF-8"));

System.out.println(URLEncoder.encode("This\"string\"has\"quote\"marks", "UTF-8"));

System.out.println(URLEncoder.encode("This:string:has:colons","UTF-8"));

System.out.println(URLEncoder.encode("This~string~has~tildes","UTF-8"));

System.out.println(URLEncoder.encode("This(string)has(parentheses)", "UTF-8"));

System.out.println(URLEncoder.encode("This.string.has.periods","UTF-8"));

System.out.println(URLEncoder.encode("This=string=has=equals=signs", "UTF-8"));

System.out.println(URLEncoder.encode("This&string&has&ersands","UTF-8"));

System.out.println(URLEncoder.encode("Thiséstringéhasé non-ASCII characters","UTF-8"));

// System.out.println(URLEncoder.encode("this中华人民共和国","UTF-8"));

} catch (UnsupportedEncodingException ex) {throw new RuntimeException("Broken VM does not support UTF-8");

}

}

}

下面就是它的输出。需要注意的是这些代码应该以其他编码形式被保存而不是以ASCII码的形式,还有就是你选择的编码形式应该作为一个参数传给编译器,让编译器能据此对源代码中的非ASCII字符作出正确的解释。

% javac -encoding UTF8 EncoderTest %

java EncoderTest

This+string+has+spaces

This*string*has*asterisks

This%25string%25has%25percent%25signs

This%2Bstring%2Bhas%2Bpluses

This%2Fstring%2Fhas%2Fslashes

This%22string%22has%22quote%22marks

This%3Astring%3Ahas%3Acolons

This%7Estring%7Ehas%7Etildes

This%28string%29has%28parentheses%29

This.string.has.periods

This%3Dstring%3Dhas%3Dequals%3Dsigns

This%26string%26has%26ampersands

This%C3%A9string%C3%A9has%C3%A9non-ASCII+characters

特别需要注意的是这个方法编码了符号,“\” ,&,=,和:。它不会尝试着去规定在一个URL中这些字符怎样被使用。由此,所以不得不分块编码URL,而不是把整个URL一次传给这个方法。这是很重要的,因为对类URLEncoder最通常的用法就是查询string,为了和服务器端使用GET方法的程序进行交互。例如,假设你想编码这个查询sting,它用来搜索某网站:

pg=q&kl=XX&stype=stext&q=+"Java+I/O"&search.x=38&search.y=3

这段代码对其进行编码:

String query = URLEncoder.encode( "pg=q&kl=XX&stype=stext&q=+\"Java+I/O\"&search.x=38&search.y=3");

System.out.println(query);

可是,得到的输出是:

pg%3Dq%26kl%3DXX%26stype%3Dstext%26q%3D%2B%22Java%2BI%2FO%22%26search.x%3D38%26search.y%3D3

出现这个问题就是方法URLEncoder.encode( ) 在进行盲目地编码。它不能区分在URL或者查询string中被用到的特殊字符(象前面string中的“=”,和“&”)和确实需要被编码的字符。由此,所以URL需要像下面这样一次只编码一块:

String query = URLEncoder.encode("pg");

query += "=";

query += URLEncoder.encode("q");

query += "&";

query += URLEncoder.encode("kl");

query += "=";

query += URLEncoder.encode("XX");

query += "&";

query += URLEncoder.encode("stype");

query += "=";

query += URLEncoder.encode("stext");

query += "&";

query += URLEncoder.encode("q");

query += "=";

query += URLEncoder.encode("\"Java I/O\"");

query += "&";

query += URLEncoder.encode("search.x");

query += "=";

query += URLEncoder.encode("38");

query += "&";

query += URLEncoder.encode("search.y");

query += "=";

query += URLEncoder.encode("3");

System.out.println(query);

这才是真正想得到的输出:

pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3

二、URLDecoder

与URLEncoder 类相对应的URLDecoder 类有两种静态方法。它们解码以x-www-form-url-encoded这种形式编码的string。也就是说,它们把所有的加号(+)转换成空格符,把所有的%xx分别转换成与之相对应的字符:

public static String decode(String s) throws Exception

public static String decode(String s, String encoding) // Java 1.4 throws UnsupportedEncodingException

如果string包含了一个“%”,但紧跟其后的不是两位16进制的数或者被解码成非法序列,该方法就会抛出 IllegalArgumentException 异常。当下次再出现这种情况时,它可能就不会被抛出了。这是与运行环境相关的,当检查到有非法序列时,抛不抛出 IllegalArgumentException 异常,这时到底会发生什么是不确定的。

由于这个方法没有触及到非转义字符,所以你可以把整个URL作为参数传给该方法,不用像之前那样分块进行。例如:

String input = "http://www.oschina.net/cgi-bin/" + "query?pg=q&kl=XX&stype=stext&q=%2B%22Java+I%2FO%22&search.x=38&search.y=3";

try {

String output = URLDecoder.decode(input, "UTF-8");

System.out.println(output);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  URL编码