天天看点

java网络编程之application/x-www-form-urlencoded MIME

URLEncoder和URLDecoder用于完成普通字符串和application/x-www-form-urlencoded MIME字符串之间的相互转换.

java网络编程之application/x-www-form-urlencoded MIME

编码和解码:

  在Web的浏览器中,不同的浏览器的编码和解码规则是不一样的.

  对于W3C浏览器:遵循W3C组织规范的浏览器,(非IE).

               编码:byte[] data = “杨哥”.getByte(String charsetName);

               解码:String  str = new String(data, String charsetName);

   对于IE浏览器:

              编码使用的application/x-www-form-urlencoded MIME机制.

import java.net.URLDecoder;
import java.net.URLEncoder;

public class URLecodeDemo {
	public static void main(String[] args) throws Exception {
		String name = "杨哥 sunshine";
		//编码
		String encode = URLEncoder.encode(name, "UTF-8");
		System.out.println(encode);//%E6%9D%A8%E5%93%A5+sunshine
		//解码
		String decode = URLDecoder.decode(encode, "UTF-8");
		System.out.println(decode);//杨哥 sunshine
	}
}