zoukankan      html  css  js  c++  java
  • 网址规范函数完成(Url Normalize)即相对地址的补全

    接上文  url的规范化问题, 今天有空把这个函数完成了。基本没有对输入做检测,所以用的时候请注意哦

    函数功能:将相对网址,根据页面地址补全为绝对网址.

     class Program
    {
    static void Main(string[] args)
    {

    string baseUrl1 = "http://aifunv.com/?p=18";
    string url = "?p=2";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://aifunv.com/";
    url = "?p=2";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com/download/editor.htm";
    url = "../sitemap.html";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com/download/temp/editor.htm";
    url = "http://www.cnblogs.com/sitemap.html";//
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com/";
    url = "#top";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com/index";
    url = "/page1.html";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com/index/";
    url = "page1.html";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    baseUrl1 = "http://www.aifunv.com";
    url = "//l.aifunv.com/spirit/logo.gif";
    Console.WriteLine(NormalizeUrl(baseUrl1, url));

    Console.ReadKey();
    }

    /// <summary>
    /// 规范网址
    /// </summary>
    /// <param name="BaseUrl"></param>
    /// <param name="Url"></param>
    /// <returns></returns>
    public static string NormalizeUrl(string BaseUrl, string Url)
    {
    // <a href="?pageNo=2">第 2 頁</a>
    if (Url.StartsWith("?"))
    {
    int index = BaseUrl.IndexOf("?");
    if (index > 0)
    {
    return BaseUrl.Substring(0, index) + Url;
    }
    else
    {
    return BaseUrl + Url;
    }
    }

    // # 上層目錄的 sitemap.aspx 頁面
    // <a href="../sitemap.aspx
    //-----------
    // # 上兩層目錄的 default.htm 頁面
    // <a href="http://www.cnblogs.com/default.htm
    //-----------
    // # 上層目錄下的 images 目錄下的 dot 目錄下的 red.gif 檔案
    // <a href="../images/dot/red.gif
    if (Url.StartsWith("../"))
    {
    string temp = Url;

    int lastIndex = BaseUrl.LastIndexOf("/");
    BaseUrl = BaseUrl.Substring(0, lastIndex);

    while (Url.StartsWith("../"))
    {
    lastIndex = BaseUrl.LastIndexOf("/");
    BaseUrl = BaseUrl.Substring(0, lastIndex);
    Url = Url.Substring(3);
    }
    return BaseUrl + "/" + Url;
    }

    //<img src="//l.yimg.com/tw.yimg.com/i/tw/hp/spirit/yahoo_logo.gif" />
    if (Url.StartsWith("//"))
    {
    int lastIndex = BaseUrl.LastIndexOf("//");
    if (lastIndex > 0)
    {
    BaseUrl = BaseUrl.Substring(0, lastIndex);
    }
    return BaseUrl + Url;
    }

    // <a href="/index.aspx
    if (Url.StartsWith("/"))
    {
    int lastIndex = BaseUrl.LastIndexOf("/");
    if (lastIndex > 0)
    {
    BaseUrl = BaseUrl.Substring(0, lastIndex);
    }
    return BaseUrl + Url;
    }

    // <a href="#top
    if (Url.StartsWith("#"))
    {
    int lastIndex = BaseUrl.LastIndexOf("#");
    if (lastIndex > 0)
    {
    BaseUrl = BaseUrl.Substring(0, lastIndex);
    }
    return BaseUrl + Url;
    }

    // # 同目錄下的 step2.aspx 頁面
    // <a href="step2.aspx
    int _lastIndex = BaseUrl.LastIndexOf("/");
    if (_lastIndex > 0)
    {
    BaseUrl = BaseUrl.Substring(0, _lastIndex + 1);
    }
    return BaseUrl + Url;

    }
    }

    我的博客哦

  • 相关阅读:
    【BZOJ5298】【CQOI2018】交错序列(矩阵快速幂优化dp)
    【BZOJ5297】【CQOI2018】社交网络(有向图生成树计数)
    【BZOJ5296】【CQOI2018】破解D-H协议(BSGS)
    【BZOJ1185】【HNOI2007】最小矩形覆盖(凸包+旋转卡壳)
    【BZOJ1069】【SCOI2007】—最大土地面积(凸包+旋转卡壳)
    【BZOJ2300】【HAOI2011】—防线修建(set维护动态凸包)
    【POJ1912】【Ceoi2002】—A highway and the seven dwarfs(凸包)
    【BZOJ1043】【HAOI2008】—下落的圆盘(圆的并集)
    node-多进程
    Node-RESTful
  • 原文地址:https://www.cnblogs.com/yczz/p/2385274.html
Copyright © 2011-2022 走看看