我正在尝试构建http://IP:4567/foldername/1234?abc=xyz
。我对此知之甚少,但是我通过谷歌搜索编写了如下代码:
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class MyUrlConstruct {
public static void main(String a[]){
try {
String protocol = "http";
String host = "IP";
int port = 4567;
String path = "foldername/1234";
URL url = new URL (protocol, host, port, path);
System.out.println(url.toString()+"?");
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
}
我能够构建URL http://IP:port/foldername/1234?
,但目前卡在查询部分,请帮助我继续前进。
abc=xyz
包含其他的=、?或&
等字符,它将会被破坏。建议使用专门的库来处理这个问题,当然在极其简单的情况下,拼接也可以使用。 - Christophe Roussyjavax.ws.rs.core.UriBuilder
,可以为您完成此操作。 - Keeley Hoek