23

I want to build the following URI -

https://10.112.88.182:8443/Vehicle/services/socialService/login

...

Builder builder = new Builder();
builder.scheme(Constants.URL_SCHEME);
builder.authority(host);
builder.appendPath(service + "/" +method);
return builder.build().toString();

where

  • URL_SCHEME - https
  • host - 10.112.88.182:8443/Vehicle/services/
  • service - socialService
  • method - login

When this code runs I get the following URI -

https://10.112.88.182%3A8443%2FVehicle%2Fservices%2F/socialService%2Flogin

/ is replaced by %2F and : is replaced by %3A

4

3 回答 3

38

就是这样Uri.Builder工作的。它将具有特殊含义的非安全 URL 字符编码为%xx十六进制值。

要防止对已经正确编码的 URI 部分进行编码,请使用encoded构建器函数的版本:

builder.encodedAuthority(host);
builder.appendEncodedPath(service + "/" +method);

但是由于您的所有 URL 部分都已准备好并且不需要任何进一步的编码,因此使用常规StringBuilder连接部分更容易。

于 2013-09-06T09:46:00.220 回答
10

就像 laalto 说的那样,这就是 Uri.Builder 的工作方式,但是如果您想以常规 url 形式获取 uri,就像您的情况一样:https://10.112.88.182:8443/Vehicle/services/socialService/login 您可以这样做:

URL url = new URL(URLDecoder.decode(builder.build().toString(), "UTF-8"));
于 2015-07-24T15:41:28.113 回答
3

解决方案非常简单,只需使用appendEncodedPath(),它不会对您的字符串进行编码,它只是按原样附加它。

于 2017-10-17T15:26:13.480 回答