关于http url中特殊符号的记录
Posted Airsolstice
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于http url中特殊符号的记录相关的知识,希望对你有一定的参考价值。
在java中编写http 请求时,很多时候需要注意编码问题。因为在url中有如下特殊字符需要注意
空格 - %20
" - %22
# - %23
% - %25
& - %26
( - %28
) - %29
+ - %2B
, - %2C
/ - %2F
: - %3A
; - %3B
< - %3C
= - %3D
> - %3E
? - %3F
@ - %40
\\ - %5C
| - %7C
在处理过程中,需要使用URLEncode进行转化。但是需要注意的时,不能直接将一条get请求的URL直接一次性转化,这样会导致各种意想不到的乱码。正确的做法是,以=号为分割界限,将有可能涉及特殊字符的value值进行编码。这样做,使服务端不会因为字段是乱码而接收不到传递的参数
public static String encode(String str) throws UnsupportedEncodingException
return URLEncoder.encode(str, "utf-8");
params
.append("fromJson=")
.append(encode("[\\"id\\":30,"
+ "\\"name\\":\\"YUEHANTEST\\","
+ "\\"catalog\\":\\"\\","
+ "\\"schema\\":\\"YUEHANTEST\\","
+ "\\"tableName\\":\\"db_info\\","
+ "\\"whereSql\\":\\"\\","
+ "\\"cols\\":\\"\\\\\\"id\\\\\\","
+ "\\\\\\"conn_name\\\\\\","
+ "\\\\\\"db_type\\\\\\","
+ "\\\\\\"url\\\\\\","
+ "\\\\\\"user_name\\\\\\","
+ "\\\\\\"password\\\\\\","
+ "\\\\\\"host\\\\\\","
+ "\\\\\\"port\\\\\\","
+ "\\\\\\"db_name\\\\\\","
+ "\\\\\\"note\\\\\\","
+ "\\\\\\"del_flg\\\\\\","
+ "\\\\\\"create_time\\\\\\","
+ "\\\\\\"update_time\\\\\\"\\"]")).append("&")
.append("toDbJson=")
.append(encode("[\\"cols\\":\\"\\\\\\"id\\\\\\","
+ "\\\\\\"conn_name\\\\\\","
+ "\\\\\\"db_type\\\\\\","
+ "\\\\\\"url\\\\\\","
+ "\\\\\\"user_name\\\\\\","
+ "\\\\\\"password\\\\\\","
+ "\\\\\\"host\\\\\\","
+ "\\\\\\"port\\\\\\","
+ "\\\\\\"db_name\\\\\\","
+ "\\\\\\"note\\\\\\","
+ "\\\\\\"del_flg\\\\\\","
+ "\\\\\\"create_time\\\\\\","
+ "\\\\\\"update_time\\\\\\"\\","
+ "\\"createSql\\":\\"\\","
+ "\\"tableName\\":\\"db_info\\","
+ "\\"name\\":\\"kdl_test\\","
+ "\\"replaceEnter\\":true,"
+ "\\"replacement\\":\\" \\","
+ "\\"id\\":\\"26\\","
+ "\\"catalog\\":\\"kdl_test\\","
+ "\\"schema\\":null]"));
以上是关于关于http url中特殊符号的记录的主要内容,如果未能解决你的问题,请参考以下文章