简体   繁体   English

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

[英]Extract host name/domain name from URL string

I have a URL like http://hostname:port_no/control/login.jsp .我有一个像http://hostname:port_no/control/login.jsp这样的 URL。

I have the above url stored in some String.Now, I need to extract hostname from 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.我想知道是否有更好的方法来做同样的事情。

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 url is invalid (undefined hostname), it returns with null.如果给定的url无效(未定义的主机名),则返回 null。

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();
}
java.net.URL u = new URL("http://hostname:port_no/control/login.jsp");
System.err.println(u.getHost());

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];

In Java:在 Java 中:

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

Hope this helps.希望这可以帮助。

@KarelG's answer is the best answer, though I had specific issues with certain non-standard domains. @KarelG 的答案是最好的答案,尽管我对某些非标准域有具体问题。 The example issue is self contained below.示例问题在下面是自包含的。

For certain "real world" input values, I had to add a check to the URI scheme to avoid mis-parsing of some addresses.对于某些“真实世界”的输入值,我必须向 URI 方案添加一个检查,以避免错误解析某些地址。 This is the changed code.这是更改后的代码。

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);
    }
}

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) 是主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM