Build URL in java

前端 未结 5 1478
故里飘歌
故里飘歌 2021-02-05 00:56

Trying to build http://IP:4567/foldername/1234?abc=xyz. I don\'t know much about it but I wrote below code from searching from google:

import java.n         


        
5条回答
  •  情书的邮戳
    2021-02-05 01:28

    You can just pass raw spec

    new URL("http://IP:4567/foldername/1234?abc=xyz");
    

    Or you can take something like org.apache.http.client.utils.URIBuilder and build it in safe manner with proper url encoding

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http");
    builder.setHost("IP");
    builder.setPath("/foldername/1234");
    builder.addParameter("abc", "xyz");
    URL url = builder.build().toURL();
    

提交回复
热议问题