且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java的URL/URI无法正确解析以?开头的链接. (审讯点)

更新时间:2023-11-26 20:04:40

这将很好地工作:

public static void main(String[] args) throws Exception {
    String base = "http://abc.com.br/index.php?hello=world";
    String relative = "?test=xyz";

    System.out.println(new URL(new URL(base), relative).toExternalForm());
    // http://abc.com.br/?test=xyz

    System.out.println((new URI(base)).resolve(relative).toString());
    // http://abc.com.br/?test=xyz

    System.out.println(org.apache.http.client.utils.URIUtils.resolve(new URI(base), relative).toString());
    // http://abc.com.br/index.php?test=xyz
}

URIUtils位于org.apache.httpcomponents:httpclient 4.0或更高版本中.

URIUtils live in org.apache.httpcomponents:httpclient version 4.0 or higher.