发送get请求报错: java.net.URISyntaxException: Illegal character in scheme name

当使用HttpGet发送

xxx.xxx.xxx.xxx:2070//common_score_api.php?UserResources=[{"dataColumn":"yearmonth","dbName":"house","id":8,"tableName":"personal_wide_vars","uid":50}]&format=json&account_no=3DF7Y1B5F4D64277

类似于上面的请求时,如果不对请求的进行处理,就会发生错误

这是由于url含有"[","{"等特殊字符,在url中不能直接使用,需要对其进行转义。

下面是转义的方法

queryUrl = queryUrl.replace("\"", "%22")
                                        .replace("[", "%5B")
                                        .replace("]", "%5D")
                                        .replace("{", "%7B")
                                        .replace("}", "%7D");

请求的路径经过这样处理就可以了。

如果使用UrlUtil.encode的话,有如下注意事项

注意事项:

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character 

URLEncoder 必须仅仅编码参数或者参数的值,不能编码整个url,也不能一起对 param=value 进行编码。而是应该: param=URLEncode(value, "utf-8")

因为 url 中的 & 和 = 他们是作为参数之间 以及 参数和值之间的分隔符的。如果一起编码了,就无法区分他们了。

如果参数过多的话,使用UrlUtil.encode就很麻烦了

猜你喜欢

转载自blog.csdn.net/chaos_le/article/details/81449236
今日推荐