Encode and Decode rfc2396 URLs

前端 未结 4 1350
[愿得一人]
[愿得一人] 2020-12-15 10:46

What is the best way to encode URL strings such that they are rfc2396 compliant and to decode a rfc2396 compliant string such that for example %20 is replaced with a space c

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 11:13

    The javadocs recommend using the java.net.URI class to accomplish the encoding. To ensure that the URI class properly encodes the url, one of the multi-argument constructors must be used. These constructors will perform the required encoding, but require you to parse any url string into the parameters.

    If you want to decode, you must construct the URI with the single argument constructor, which does not do any encoding. You can then call methods such as getPath() etc. to retrieve and build the decoded URL.

提交回复
热议问题