java.net.URISyntaxException: 路径中索引 75 处的非法字符

java.net.URISyntaxException: Illegal character in path at index 75

我正在尝试通过 Apache REST 客户端从 java 发送 GET 请求,但遇到了这个问题。

java.net.URISyntaxException: Illegal character in path at index 75: http://torrento.sharepoint.com/_api/web/getfolderbyserverrelativeurl('/Shared Documents/test')/files at java.net.URI$Parser.fail(URI.java:2848) at java.net.URI$Parser.checkChars(URI.java:3021) at java.net.URI$Parser.parseHierarchical(URI.java:3105) at java.net.URI$Parser.parse(URI.java:3053) at java.net.URI.(URI.java:588) at org.apache.http.client.utils.URIBuilder.(URIBuilder.java:82) at com.mstack.samples.sharepoint.SharepointApp.getAllFiles(SharepointApp.java:61) at com.mstack.samples.sharepoint.SharepointApp.main(SharepointApp.java:45)

代码片段:-

            httpClient = HttpClientBuilder.create().build();
            uriBuilder = new URIBuilder(requestUrl);
            System.out.println(uriBuilder);
            httpGet = new HttpGet(uriBuilder.build());
            httpGet.addHeader(AUTHORIZATION, "Bearer " + TOKEN);
            httpGet.addHeader("accept", "application/json; odata=verbose");
            response = httpClient.execute(httpGet);

其中requestUrl是http://torrento.sharepoint.com/_api/web/getfolderbyserverrelativeurl('/Shared Documents/test')/files

我知道共享和文档之间的 space 是问题所在。试图对其进行编码。但这也没有用。请帮助

我通过简单地添加 requestUrl.replaceAll(" ", "%20"); 获得了解决方案 但如果是其他特殊字符,仅此一项是行不通的。所以我们必须在发送请求之前编码url。

干杯:)