在提出请求时收到错误

时间:2012-03-29 19:17:58

标签: grails groovy

我的URI中有以下内容

        uri.query = [id: "urn://salina/search/xplorer/XplorerApp",criteria:"{'includeNonGeotagged':'true','freetext':\{'text':" + "'" + "${searchString}" +"'}}",contentType:"rss20"]

我收到一条错误,说java.net.URISyntaxException:索引94的查询中出现非法字符:https://pix.us.baesystems.com/search/query?id=urn://salina/search/xplorer/XplorerApp&criteria= {'includeNonGeotagged':'true','freetext':{'text':'test'}} &安培;的contentType = rss20

当我转储uri.toString()的值并将其放在浏览器中时,它工作正常,所以我无法弄清楚这里的问题是什么......

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为您的URI中有未编码的字符。尝试对它们进行编码或首先从字符串中创建一个URL:

def url = "https://pix.us.baesystems.com/search/query?id=urn://salina/search/xplorer/XplorerApp&criteria={'includeNonGeotagged':'true','freetext':  {'text':'test'}}&contentType=rss20"
URL url = new URL(url);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
println uri    

这将打印正确的URI:

https://pix.us.baesystems.com/search/query?id=urn://salina/search/xplorer/XplorerApp&criteria=%7B'includeNonGeotagged':'true','freetext':%7B'text':'test'%7D%7D&contentType=rss20

另请参阅this answer了解基本想法。