url中的汉字encode转换,为什么?

发现现在几乎所有的网站都对url中的汉字和特殊的字符,进行了urlencode操作,也就是:

http://hi.baidu.com/%BE%B2%D0%C4%C0%CF%C8%CB/creat/blog/

这个样子,中间%形式的,肯定就是我的登录用户名称了吧。

为什么对这些字符进行了u的编码形式,是为了字符编码(gbk、utf8)还是为了不出现特殊的字符在url中?都知道要转,但是转了的真正好处呢。查看了网上的很多资料,也没有找到更加准确的说法。

url转义其实也只是为了符合url的规范而已。因为在标准的url规范中中文和很多的字符是不允许出现在url中的。

看一下php的urlencode的说明了。

urlencode — 编码 URL 字符串

string urlencode ( string $str )

返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。此编码与 WWW 表单 POST 数据的编码方式是一样的,同时与application/x-www-form-urlencoded 的媒体类型编码方式一样。由于历史原因,此编码在将空格编码为加号(+)方面与 RFC1738 编码(参见rawurlencode())不同。此函数便于将字符串编码并将其用于 URL 的请求部分,同时它还便于将变量传递给下一页。

标准的英文说明是:

"...Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL."

那哪些字符是需要转化的呢?

1. ASCII 的控制字符

这些字符都是不可打印的,自然需要进行转化。

2. 一些非ASCII字符

这些字符自然是非法的字符范围。转化也是理所当然的了。

3. 一些保留字符

很明显最常见的就是“&”了,这个如果出现在url中了,那你认为是url中的一个字符呢,还是特殊的参数分割用的呢?

4. 就是一些不安全的字符了。

例如:空格。为了防止引起歧义,需要被转化为“+”。

明白了这些,也就知道了为什么需要转化了,而转化的规则也是很简单的。

按照每个字符对应的字符编码,不是符合我们范围的,统统的转化为%的形式也就是了。自然也是16进制的形式。

和字符编码无关

通过urlencode的转化规则和目的,我们也很容易的看出,urleocode是基于字符编码的。同样的一个汉字,不同的编码类型,肯定对应不同的urleocode的串。gbk编码的有gbk的encode结果。

apache等服务器,接受到字符串后,可以进行decode,但是还是无法解决编码的问题。编码问题,还是需要靠约定或者字符编码的判断解决。

因此,urleocode只是为了url中一些非ascii字符,可以正确无误的被传输,至于使用哪种编码,就不是eocode所关心和解决的问题了。

编码问题,不是urlencode所要解决的。

代码实例:

/* * Copyright 2011 Alibaba.com All right reserved. This software is the * confidential and proprietary information of Alibaba.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Alibaba.com. */ package arraySort; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; /** * 类Encode.java的实现描述:TODO 类实现描述 * @author zhipeng.songzp 2011-9-10 下午07:44:40 */ public class Encode { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "& "; try { String changBefore=URLEncoder.encode(str, "GBK"); System.out.println(changBefore); String changeAfter=URLDecoder.decode(changBefore); System.out.println(changeAfter); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }


运行结果:

%26+ &



当然也可以手动来实现编码转换:

offerSearchUrl.append(StringEscapeUtil.escapeURL(search.getKeywords(), "GBK"));


/** * 将指定字符串编码成<code>application/x-www-form-urlencoded</code>格式。 * <p> * 除了RFC2396中的<code>unreserved</code>字符之外的所有字符,都将被转换成URL编码<code>%xx</code>。 根据RFC2396,<code>unreserved</code>的定义如下: * * <pre> * <![CDATA * unreserved = alphanum | mark * alphanum = 大小写英文字母 | 数字 * mark = "-" | "_" | "." | "!" | "˜" | "*" | "'" | "(" | ")" * ]]> * </pre> * * </p> * <p> * 该方法使用指定的字符编码来编码URL。 * </p> * * @param str 要编码的字符串,可以是<code>null</code> * @param encoding 输出字符编码,如果为<code>null</code>,则使用系统默认编码 * @return URL编码后的字符串 * @throws UnsupportedEncodingException 如果指定的<code>encoding</code>为非法的 */ public static String escapeURL(String str, String encoding) throws UnsupportedEncodingException { return escapeURLInternal(str, encoding, true); }


/** * 将指定字符串编码成<code>application/x-www-form-urlencoded</code>格式。 * * @param str 要编码的字符串,可以是<code>null</code> * @param encoding 输出字符编码,如果为<code>null</code>,则使用系统默认编码 * @param strict 是否以严格的方式编码URL * @return URL编码后的字符串 * @throws UnsupportedEncodingException 如果指定的<code>encoding</code>为非法的 */ private static String escapeURLInternal(String str, String encoding, boolean strict) throws UnsupportedEncodingException { if (str == null) { return null; } try { StringWriter out = new StringWriter(str.length()); if (escapeURLInternal(str, encoding, out, strict)) { return out.toString(); } return str; } catch (UnsupportedEncodingException e) { throw e; } catch (IOException e) { return str; // StringWriter不可能发生这个异常 } }


/** * 将指定字符串编码成<code>application/x-www-form-urlencoded</code>格式。 * * @param str 要编码的字符串,可以是<code>null</code> * @param encoding 输出字符编码,如果为<code>null</code>,则使用系统默认编码 * @param strict 是否以严格的方式编码URL * @param out 输出流 * @return 如果字符串被改变,则返回<code>true</code> * @throws IOException 如果输出到<code>out</code>失败 * @throws UnsupportedEncodingException 如果指定的<code>encoding</code>为非法的 * @throws IllegalArgumentException <code>out</code>为<code>null</code> */ private static boolean escapeURLInternal(String str, String encoding, Writer out, boolean strict) throws IOException { if (encoding == null) { encoding = LocaleUtil.getContext().getCharset(); } boolean needToChange = false; if (out == null) { throw new IllegalArgumentException("The Writer must not be null"); } if (str == null) { return needToChange; } // 用来将字符=>字节的临时空间,长度为10足矣。 ByteArrayOutputStream baos = new ByteArrayOutputStream(10); OutputStreamWriter writer = new OutputStreamWriter(baos, encoding); for (int i = 0; i < str.length(); i++) { int ch = str.charAt(i); if (isSafeCharacter(ch, strict)) { // “安全”的字符,直接输出 out.write(ch); } else if (ch == ' ') { // 特殊情况:空格(0x20)转换成'+' out.write('+'); // 设置改变标志 needToChange = true; } else { // 对ch进行URL编码。 // 首先按指定encoding取得该字符的字节码。 try { writer.write(ch); writer.flush(); } catch (IOException e) { baos.reset(); continue; } byte[] bytes = baos.toByteArray(); for (int j = 0; j < bytes.length; j++) { byte toEscape = bytes[j]; out.write('%'); int low = (toEscape & 0x0F); int high = (toEscape & 0xF0) >> 4; out.write(HEXADECIMAL[high]); out.write(HEXADECIMAL[low]); } baos.reset(); // 设置改变标志 needToChange = true; } } return needToChange; }


/** * 判断指定字符是否是“安全”的,这个字符将不被转换成URL编码。 * * @param ch 要判断的字符 * @param strict 是否以严格的方式编码 * @return 如果是“安全”的,则返回<code>true</code> */ private static boolean isSafeCharacter(int ch, boolean strict) { if (strict) { return UNRESERVED.get(ch); } else { return (ch > ' ') && !RESERVED.get(ch) && !Character.isWhitespace((char) ch); } }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值