Java 从 URL 字符串中提取主机名/域名

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

Extract host name/domain name from URL string

javastringurl

提问by Anand

I have a URL like http://hostname:port_no/control/login.jsp.

我有一个像http://hostname:port_no/control/login.jsp.

I have the above url stored in some String.Now, I need to extract hostnamefrom the String.

我将上面的 url 存储在一些hostname字符串中。现在,我需要从字符串中提取。

I am doing like this in my Java code

我在我的 Java 代码中这样做

String domain = url.substring(url.indexOf('/') + 2, url.lastIndexOf(':'));

I want to know if there is any better way to do the same.

我想知道是否有更好的方法来做同样的事情。

采纳答案by KarelG

You can use the java.net.URI-class to extract the hostname from the string.

您可以使用java.net.URI-class 从字符串中提取主机名。

Here below is a method from which you can extract your hostname from a string.

下面是一种可以从字符串中提取主机名的方法。

public String getHostName(String url) {
    URI uri = new URI(url);
    String hostname = uri.getHost();
    // to provide faultproof result, check if not null then return only hostname, without www.
    if (hostname != null) {
        return hostname.startsWith("www.") ? hostname.substring(4) : hostname;
    }
    return hostname;
}

This above gives you the hostname, and is faultproof if your hostname does start with either hostname.com/...or www.hostname.com/..., which will return with 'hostname'.

以上为您提供了主机名,并且如果您的主机名确实以hostname.com/...或开头www.hostname.com/...,则它是万无一失的,它将以“主机名”返回。

If the given urlis invalid (undefined hostname), it returns with null.

如果给定url无效(未定义的主机名),则返回 null。

回答by PeterMmm

java.net.URL u = new URL("http://hostname:port_no/control/login.jsp");
System.err.println(u.getHost());

回答by niiraj874u

java.net.URL aURL;
try {
    aURL = new java.net.URL("http://example.com:80/docs/");
    System.out.println("host = " + aURL.getHost()); //example.com
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

回答by astack

If you want string work, then try the following code sample,

如果您想要字符串工作,请尝试以下代码示例,

String URL= "http://hostname:port_no/control/login.jsp";
String s_URL[] = ULR.split("//");
String s1 = s_URL[1];
String s2[] = s1.split(":");
String hostname = s2[0];

回答by luiscosta

In Java:

在 Java 中:

String hostname = url.split("://")[1].split(":")[0];
String portnumber = url.split("://")[1].split(":")[1].split("/")[0];

Hope this helps.

希望这可以帮助。

回答by Gregory Alan Bolcer

@KarelG's answer is the best answer, though I had specific issues with certain non-standard domains. The example issue is self contained below.

@KarelG 的答案是最好的答案,尽管我对某些非标准域有具体问题。示例问题在下面是自包含的。

For certain "real world" input values, I had to add a check to the URI scheme to avoid mis-parsing of some addresses. This is the changed code.

对于某些“真实世界”的输入值,我必须向 URI 方案添加一个检查,以避免错误解析某些地址。这是更改后的代码。

import java.net.URI;
import java.net.*;

public class Domain {
    public static String getDomainName(String url) {
        try {
            URI uri = new URI(url);
            String domain = uri.getHost();
            System.out.println("domain: " + domain);
            if (uri.getScheme() != null) {
                return domain.startsWith("www.") ? domain.substring(4) : domain;
            } else {
                return uri.getSchemeSpecificPart();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

Below is the test case and value that was failing.

下面是失败的测试用例和值。

Domain.java javac Domain.java java Domain

域.java javac 域.java java 域

import java.net.URI;
import java.net.*;
import java.io.*;

public class Domain {

    public static String longname = "https://www.3dprintingmedia.network/longform/page.html";
    public static String name = "www.3dprintingmedia.network";

    public static void getDomain(String url) {
        try {
                        URI uri = new URI(url);
                        String domain = uri.getHost();
            System.out.println("protocol: " + uri.getScheme());
            System.out.println("path: " + uri.getPath());
                        System.out.println("name: " + name);
                        System.out.println("domain: " + domain);
                        System.out.println(domain.startsWith("www.") ? domain.substring(4) : domain);
        } catch (Exception e) {
                        e.printStackTrace();
        }
   }

    public static void main(String[] args) {
       System.out.println("Parsing domain: " + name); 
       getDomain(longname);
       getDomain(name);
       System.exit(0);
    }
}

回答by Freddie

A solution using Regular expression and group:

使用正则表达式和组的解决方案:

    String pattern = "(\w*://)([\w-_.]+)([:\w\W]*)";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(a);
    if (m.find())
    {
        System.out.println(m.group(0));
        System.out.println(m.group(1));
        System.out.println(m.group(2));
        System.out.println(m.group(3));
    }

group(2) is the hostname.

group(2) 是主机名。