java – 如何从任何字符串url获取网站名称

我给了String,其中包含任何有效的URL.
我必须从给定的URL找到网站的名称.
我也忽略了子域名.

喜欢

http://www.yahoo.com   =>    yahoo
www.google.co.in =>      google
http://in.com    =>      in
http://india.gov.in/ => india
https://in.yahoo.com/ => yahoo
http://philotheoristic.tumblr.com/  =>tumblr
http://philotheoristic.tumblr.com/
https://in.movies.yahoo.com/        =>yahoo

这个怎么做

解决方法:

哟可以使用URL

来自文档 – http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

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

public class ParseURL {
    public static void main(String[] args) throws MalformedURLException {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }
}

这是程序显示的输出:

protocol = http
authority = example.com:80
host = example.com                     // name of website
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

因此,通过使用aURL.getHost(),您可以获得网站名称.要忽略子域,可以使用“.”将其拆分.因此它变为aURL.getHost().split(“.”)[0]以获得唯一的名称.

上一篇:Activemq服务无法启动的报错及原因总结


下一篇:HTML5_音视频标签