如何在 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(); }