在java中构建URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39498767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:12:14  来源:igfitidea点击:

Build URL in java

javaurlbuild

提问by rrr ppp

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:

试图建立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();
        }
    }
}

I am able to build URL http://IP:port/foldername/1234?. I am stuck at query part. Please help me to move forward.

我能够建立 URL http://IP:port/foldername/1234?。我被困在查询部分。请帮助我前进。

采纳答案by VGR

In general non-Java terms, a URL is a specialized type of URI. You can use the URIclass (which is more modern than the venerable URL class, which has been around since Java 1.0) to create a URI more reliably, and you can convert it to a URL with the toURLmethod of URI:

在一般的非 Java 术语中,URL 是一种特殊类型的 URI。您可以使用URI类(它比古老的 URL 类更现代,自 Java 1.0 以来就存在)来更可靠地创建 URI,并且您可以使用URI的toURL方法将其转换为 URL :

String protocol = "http";
String host = "example.com";
int port = 4567;
String path = "/foldername/1234";
String auth = null;
String fragment = null;
URI uri = new URI(protocol, auth, host, port, path, query, fragment);
URL url = uri.toURL();

Note that the pathneeds to start with a slash.

注意path需要以斜线开头。

回答by vsminkov

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.URIBuilderand build it in safe manner with proper url encoding

或者您可以采用类似的org.apache.http.client.utils.URIBuilder方法并使用正确的 url 编码以安全的方式构建它

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

回答by Tyler Long

Use OkHttp

使用 OkHttp

There is a very popular library named OkHttpwhich has been starred 20Ktimes on GitHub. With this library, you can build the url like below:

有一个非常流行的库叫OkHttp,它在 GitHub 上已经被加星了20K次。使用此库,您可以构建如下所示的 url:

import okhttp3.HttpUrl;

URL url = new HttpUrl.Builder()
    .scheme("http")
    .host("example.com")
    .port(4567)
    .addPathSegments("foldername/1234")
    .addQueryParameter("abc", "xyz")
    .build().url();

Or you can simply parse an URL:

或者你可以简单地解析一个 URL:

URL url = HttpUrl.parse("http://example.com:4567/foldername/1234?abc=xyz").url();

回答by Philippe

If you happen to be using Spring already, I have found the org.springframework.web.util.UriComponentsBuilderto be quite nifty. Here is how you would use it in your case.

如果您碰巧已经在使用 Spring,我发现org.springframework.web.util.UriComponentsBuilder它非常漂亮。以下是您将如何在您的情况下使用它。

final URL myUrl = UriComponentsBuilder
        .fromHttpUrl("http://IP:4567/foldername/1234?abc=xyz")
        .build()
        .toUri()
        .toURL();