java 爪哇| 从 URL 获取 protocol://domain.port 的 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32114153/
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-11-02 19:41:21  来源:igfitidea点击:

Java | API to get protocol://domain.port from URL

java

提问by Rupesh

I have got URL as referrer and want to get protocol and domain from it.

我有 URL 作为引用者,并想从中获取协议和域。

For example: If URL is https://test.domain.com/a/b/c.html?test=hellothen output needs to be https://test.domain.com. I have gone through http://docs.oracle.com/javase/7/docs/api/java/net/URI.htmland I can't seems to find any method that can directly do so.

例如:如果 URL 是https://test.domain.com/a/b/c.html?test=hello那么输出需要是https://test.domain.com. 我已经经历了http://docs.oracle.com/javase/7/docs/api/java/net/URI.html,我似乎无法找到任何可以直接这样做的方法。



I am not using Spring, so can't use Sprint classes (if any).

我没有使用 Spring,所以不能使用 Sprint 类(如果有的话)。



Guys I can write custom login to get port, domain and protocol from URL, but looking for API which already has this implemented and can minimize my time in testing various scenarios.

伙计们,我可以编写自定义登录以从 URL 获取端口、域和协议,但正在寻找已经实现此功能的 API,并且可以最大限度地减少我测试各种场景的时间。

采纳答案by mthmulders

Create a new URLobject using your Stringvalue and call getHost()or any other method on it, like so:

URL使用您的String值创建一个新对象并调用getHost()或任何其他方法,如下所示:

URL url = new URL("https://test.domain.com/a/b/c.html?test=hello");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();

// if the port is not explicitly specified in the input, it will be -1.
if (port == -1) {
    return String.format("%s://%s", protocol, host);
} else {
    return String.format("%s://%s:%d", protocol, host, port);
}

回答by Cardin Lee JH

To elaborate on what @Rupesh mentioned in @mthmulders answer,

详细说明@Rupesh 在@mthmulders 回答中提到的内容,

getAuthority()gives both domain and port. So you just concatenate it with getProtocol()as prefix:

getAuthority()给出域和端口。所以你只需将它与getProtocol()作为前缀连接:

URL url = new URL("https://test.domain.com/a/b/c.html?test=hello");
String protocol = url.getProtocol();
String authority = url.getAuthority();
return String.format("%s://%s", protocol, authority);

回答by vinsinraw

getAuthority() returns the host along with the port but getHost() returns only the host name. So if URL is "https://www.hello.world.com:80/x/y/z.html?test=hello", then getAuthority() return www.hello.world.com:80 while getHost() returns www.hello.world.com

getAuthority() 返回主机和端口,但 getHost() 仅返回主机名。所以如果 URL 是“ https://www.hello.world.com:80/x/y/z.html?test=hello”,那么 getAuthority() 返回 www.hello.world.com:80 而 getHost()返回 www.hello.world.com

Example

例子

URL url = new URL("https://www.hello.world.com:80/x/y/z.html?test=hello");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
String authority = url.getAuthority();

System.out.println("Host "+host);   // www.hello.world.com
System.out.println("authority "+authority);   // www.hello.world.com:80

//Determining Protocol plus host plus port (if any) url using Authority (simple single line step)
System.out.println("ProtocolHostPortURL:: "+ String.format("%s://%s", protocol, authority));

//Determining Protocol plus host plus port (if any) url using Authority (multi line step)
//If the port is not explicitly specified in the input, it will be -1.
if (port == -1) {
    System.out.println("ProtocolHostURL1:: "+ String.format("%s://%s", protocol, host));        
} else {
    System.out.println("ProtocolHostPortURL2:: "+ String.format("%s://%s:%d", protocol, host, port));    
}