从 URL 获取域/主​​机名的最快方法是什么?

编程入门 行业动态 更新时间:2024-10-24 04:42:35
本文介绍了从 URL 获取域/主​​机名的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要浏览大量的字符串 url 列表并从中提取域名.

I need to go through a large list of string url's and extract the domain name from them.

例如:

http://www.stackoverflow/questions 会提取 www.stackoverflow

我最初使用的是 new URL(theUrlString).getHost() 但 URL 对象的初始化为该过程增加了大量时间并且似乎不需要.

I originally was using new URL(theUrlString).getHost() but the URL object initialization adds a lot of time to the process and seems unneeded.

是否有一种更快的方法来提取同样可靠的主机名?

Is there a faster method to extract the host name that would be as reliable?

谢谢

我的错误,是 www.将包含在上面的域名示例中.此外,这些网址可能是 http 或 https

My mistake, yes the www. would be included in domain name example above. Also, these urls may be http or https

推荐答案

如果你想处理 https 等,我建议你这样做:

If you want to handle https etc, I suggest you do something like this:

int slashslash = url.indexOf("//") + 2;
domain = url.substring(slashslash, url.indexOf('/', slashslash));

请注意,这包括 www 部分(就像 URL.getHost() 会做的那样),它实际上是域名的一部分.

Note that this is includes the www part (just as URL.getHost() would do) which is actually part of the domain name.

通过评论请求编辑

这里有两种可能有用的方法:

Here are two methods that might be helpful:

/**
 * Will take a url such as http://www.stackoverflow and return www.stackoverflow
 * 
 * @param url
 * @return
 */
public static String getHost(String url){
    if(url == null || url.length() == 0)
        return "";

    int doubleslash = url.indexOf("//");
    if(doubleslash == -1)
        doubleslash = 0;
    else
        doubleslash += 2;

    int end = url.indexOf('/', doubleslash);
    end = end >= 0 ? end : url.length();

    int port = url.indexOf(':', doubleslash);
    end = (port > 0 && port < end) ? port : end;

    return url.substring(doubleslash, end);
}


/**  Based on : http://grepcode/file/repository.grepcode/java/ext/com.google.android/android/2.3.3_r1/android/webkit/CookieManager.java#CookieManager.getBaseDomain%28java.lang.String%29
 * Get the base domain for a given host or url. E.g. mail.google will return google
 * @param host 
 * @return 
 */
public static String getBaseDomain(String url) {
    String host = getHost(url);

    int startIndex = 0;
    int nextIndex = host.indexOf('.');
    int lastIndex = host.lastIndexOf('.');
    while (nextIndex < lastIndex) {
        startIndex = nextIndex + 1;
        nextIndex = host.indexOf('.', startIndex);
    }
    if (startIndex > 0) {
        return host.substring(startIndex);
    } else {
        return host;
    }
}

这篇关于从 URL 获取域/主​​机名的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-26 23:18:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/670312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最快   方法   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!