如何在Java中构建URL或URI?有没有惯用的方法,或者很容易做到这一点的库?
How do I build a URL or a URI in Java? Is there an idiomatic way, or libraries that easily do this?
我需要允许从请求字符串开始,解析/更改各种URL部分(方案,主机,路径) ,查询字符串)并支持添加和自动编码查询参数。
I need to allow starting from a request string, parse/change various URL parts (scheme, host, path, query string) and support adding and automatically encoding query parameters.
推荐答案使用HTTPClient运行良好。
Using HTTPClient worked well.
protected static String createUrl(List<NameValuePair> pairs) throws URIException{ HttpMethod method = new GetMethod("example"); method.setQueryString(pairs.toArray(new NameValuePair[]{})); return method.getURI().getEscapedURI(); }