我想将键值对作为查询参数附加到现有URL.虽然我可以通过检查URL是否有查询部分或片段部分来执行此操作,并通过跳过一堆if子句来执行追加,但我想知道如果通过Apache执行此操作是否有干净的方法Commons库或类似的东西.
http://example.com 将会 http://example.com?name=John
http://example.com#fragment 将会 http://example.com?name=John#fragment
http://example.com?email=john.doe@email.com 将会 http://example.com?email=john.doe@email.com&name=John
http://example.com?email=john.doe@email.com#fragment 将会 http://example.com?email=john.doe@email.com&name=John#fragment
我之前已多次运行此场景,并且我希望在不破坏URL的情况下执行此操作.
Nic*_*aly 137
有很多库可以帮助您构建URI(不要重新发明轮子).这里有三个让你入门:
import javax.ws.rs.core.UriBuilder;
...
return UriBuilder.fromUri(url).queryParam(key, value).build();
Run Code Online (Sandbox Code Playgroud)
import org.apache.http.client.utils.URIBuilder;
...
return new URIBuilder(url).addParameter(key, value).build();
Run Code Online (Sandbox Code Playgroud)
import org.springframework.web.util.UriComponentsBuilder;
...
return UriComponentsBuilder.fromUriString(url).queryParam(key, value).build().toUri();
Run Code Online (Sandbox Code Playgroud)
另请参见: GIST> URI Builder测试
Ada*_*dam 47
这可以通过使用java.net.URI类使用现有部件构造新实例来完成,这应该确保它符合URI语法.
查询部分将为null或现有字符串,因此您可以决定使用&附加另一个参数,或者启动新查询.
public class StackOverflow26177749 {
public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
URI oldUri = new URI(uri);
String newQuery = oldUri.getQuery();
if (newQuery == null) {
newQuery = appendQuery;
} else {
newQuery += "&" + appendQuery;
}
URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(),
oldUri.getPath(), newQuery, oldUri.getFragment());
return newUri;
}
public static void main(String[] args) throws Exception {
System.out.println(appendUri("http://example.com", "name=John"));
System.out.println(appendUri("http://example.com#fragment", "name=John"));
System.out.println(appendUri("http://example.com?email=john.doe@email.com", "name=John"));
System.out.println(appendUri("http://example.com?email=john.doe@email.com#fragment", "name=John"));
}
}
Run Code Online (Sandbox Code Playgroud)
产量
http://example.com?name=John
http://example.com?name=John#fragment
http://example.com?email=john.doe@email.com&name=John
http://example.com?email=john.doe@email.com&name=John#fragment
Run Code Online (Sandbox Code Playgroud)
小智 7
对于 Android,请使用: https://developer.android.com/reference/android/net/Uri#buildUpon()
URI oldUri = new URI(uri);
Uri.Builder builder = oldUri.buildUpon();
builder.appendQueryParameter("newParameter", "dummyvalue");
Uri newUri = builder.build();
Run Code Online (Sandbox Code Playgroud)
使用该URI课程.
URI用你现有的东西创建一个新的String"分解"部分,然后实例化另一个来组装修改后的url:
URI u = new URI("http://example.com?email=john@email.com&name=John#fragment");
// Modify the query: append your new parameter
StringBuilder sb = new StringBuilder(u.getQuery() == null ? "" : u.getQuery());
if (sb.length() > 0)
sb.append('&');
sb.append(URLEncoder.encode("paramName", "UTF-8"));
sb.append('=');
sb.append(URLEncoder.encode("paramValue", "UTF-8"));
// Build the new url with the modified query:
URI u2 = new URI(u.getScheme(), u.getAuthority(), u.getPath(),
sb.toString(), u.getFragment());
Run Code Online (Sandbox Code Playgroud)
我建议改进 Adam 的答案,接受 HashMap 作为参数
/**
* Append parameters to given url
* @param url
* @param parameters
* @return new String url with given parameters
* @throws URISyntaxException
*/
public static String appendToUrl(String url, HashMap<String, String> parameters) throws URISyntaxException
{
URI uri = new URI(url);
String query = uri.getQuery();
StringBuilder builder = new StringBuilder();
if (query != null)
builder.append(query);
for (Map.Entry<String, String> entry: parameters.entrySet())
{
String keyValueParam = entry.getKey() + "=" + entry.getValue();
if (!builder.toString().isEmpty())
builder.append("&");
builder.append(keyValueParam);
}
URI newUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), builder.toString(), uri.getFragment());
return newUri.toString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76159 次 |
| 最近记录: |