zoukankan      html  css  js  c++  java
  • INTRODUCTION TO URL ENCODING

    http://www.permadi.com/tutorial/urlEncoding/

     

     
    URL Encoding Background
    URL Encoding is the process of converting string into valid URL format.  Valid URL format means that the URL contains only what is termed "alpha | digit | safe | extra | escape" characters.  You can read more about the what and the whys of these terms on the World Wide Web Consortium site: http://www.w3.org/Addressing/URL/url-spec.html andhttp://www.w3.org/International/francois.yergeau.html.  

    URL encoding is normally performed to convert data passed via html forms, because such data may contain special character, such as "/", ".", "#", and so on, which could either: a) have special meanings; or b) is not a valid character for an URL; or c) could be altered during transfer.   For instance, the "#" character needs to be encoded because it has a special meaning of that of an html anchor.   The <space> character also needs to be encoded because is not allowed on a valid URL format.   Also, some characters, such as "~" might not transport properly across the internet. 

     
     

    Example
    One of the most common encounters with URL Encoding is when dealing with <form>s.  Form methods (GET and POST) perform URL Encoding implicitly.  Websites uses GET and POST methods to pass parameters between html pages.

    As an example, click the form below to see the string being URL encoded. 

      
    <form method="GET" action="example.html">
      <input type="text" name="var" size="50" value="This is a simple & short test.">
    <input type="submit"> </form>
     

    This sample <form> sends the data in the text field using the GET method, which means that the data will be appended as query string. 

    If you click the button and look at the resulting URL in the browser address bar, you should see something like this (the query string portion, which is automatically URL encoded by the browser, is shown in blue):

    http://www.permadi.com/tutorial/urlEncoding/example.html?var=This+is+a+simple+%26+short+test.

    Here, you can see that:

    • The <space> character has been URL encoded as "+".
    • The & character has been URL encoded as "%26".

    <space> character and & character are just some of the special characters that need to be encoded.  Below are some others (click the button to see the result of the encoding).

     

    Here's the query string portion, which (as before) has been encoded by the browser automatically:

      
    var=%24+%26+%3C+%3E+%3F+%3B+%23+%3A+%3D+%2C+%22+%27+%7E+%2B+%25

    As you can see, when a character is URL-encoded, it's converted as %XY, where X and Y is a number.   You will see later where these numbers come from.

    What Should be URL Encoded?
    As a rule of thumb, any non alphanumeric character should be URL encoded.  This of course applies to characters that are to be interpreted as is (ie: is not intend to have special meanings) .  In such cases, there's no harm in URL-Encoding the character,  even if the character actually does not need  to be URL-Encoded. 

    Some Common Special Characters
    Here's a table of some of often used characters and their URL encodings. 

    Character

    URL Encoded

    ; %3B
    ? %3F
    / %2F
    : %3A
    # %23
    & %26
    = %3D
    + %2B
    $ %24
    , %2C
    <space> %20 or +
    % %25
    < %3C
    > %3E
    ~ %7E
    % %25
     
     

    Note that because the <space> character is very commonly used, a special code ( the "+" sign) has been reserved as its URL encoding.  Thus the string "A B" can be URL encoded as either "A%20B" or "A+B".

    Where Does the Numbers Come From?
    The number following the % sign is the hexadecimal ASCII code of the character being encoded.  You can find an ASCII table here

    Language Support
    Most web programming languages already provide built in method to perform URL Encoding and URL Decoding.  Here are the common ones, click the method name to find more info.

    Languagege URL Encoding URL Decoding
    VBScript escape(string) unescape(string)
    .NET HttpUtility.UrlEncode HttpUtility.UrlDecode
    Java java.net.URLEncode.encode(String)
    or see this link.
    See this link.
    JavaScript escape(String) Note: does not encode '/' and '+' character unescape(String)
    PHP urlencode(string) urldecode(string)
    ASP Server.URLEncode(string) ?
    Perl uri_escape 
    Use CGI.pm module.  Link.
    uri_unescape
    Flash (MX or later) escape(expresiion) unescape(expression)
  • 相关阅读:
    干货:分布式系统详解
    如果有人问你数据库的原理,叫他看这篇文章
    MySQL的B树索引与索引优化
    优化网站性能必备的6种架构方案,你知道吗?
    【干货】手把手教你搭建一套可自动化构建的微服务框架
    你真的理解微服务架构吗
    Android Activity 半透明效果(Translucent)
    Android DatepickerDialog(日期选择器)的使用
    Android搜索自动提示功能 AutocompleteTextView
    Android动态加载ListView中的Item
  • 原文地址:https://www.cnblogs.com/wucg/p/2436004.html
Copyright © 2011-2022 走看看