• 网址规范函数完成(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;

    }
    }

    我的博客哦

  • 相关阅读:
    面向对象三大特性?
    final finally finalize区别?
    LeetCode122-买卖股票的最佳时机2(贪心算法)
    LeetCode119-杨辉三角2(题目有Bug,动态规划)
    九度OJ 1051:数字阶梯求和 (大数运算)
    九度OJ 1050:完数 (数字特性)
    九度OJ 1049:字符串去特定字符 (基础题)
    九度OJ 1048:判断三角形类型 (基础题)
    九度OJ 1047:素数判定 (素数)
    九度OJ 1046:求最大值 (基础题)
  • 原文地址:https://www.cnblogs.com/yczz/p/2385274.html
Copyright © 2020-2023  润新知