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

}
}

我的博客哦

posted @ 2012-03-08 14:55  花生!~~  阅读(1452)  评论(1编辑  收藏  举报