2006-07-11
TAG:搜索相关

using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace UrlParse
{
    
/**//// 
    
/// 分析 url 字符串中的参数信息
    
/// 

    class Class1
    
{
        
/**//// 
        
/// 应用程序的主入口点。
        
/// 

        [STAThread]
        
static void Main(string[] args)
        
{
            
string url = "http://www.xxx.com/xyz/hello.asp?a=3&typeId=45&cc=ILoveYou";
            NameValueCollection nvc;
            
string baseUrl;

            ParseUrl(url, 
out baseUrl, out nvc);

            
// output results
            Console.WriteLine("baseUrl: {0}", baseUrl);
            Console.WriteLine(
"parameters:");

            
for (int i = 0; i < nvc.Count; i++)
                Console.WriteLine(
"{0}, {1}", nvc.KeysIdea [I], nvcIdea [I]);

            Console.ReadLine();
        }


        
/**//// 
        
/// 分析 url 字符串中的参数信息
        
/// 
        
/// 输入的 URL
        
/// 输出 URL 的基础部分
        
/// 输出分析后得到的 (参数名,参数值) 的集合
        
/// 木野狐(Neil Chen)
        
/// 2005-06-23

        static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc) {
            
if (url == null)
                
throw new ArgumentNullException("url");

            nvc 
= new NameValueCollection();
            baseUrl 
= "";

            
if (url == "")
                
return;            
            
            
int questionMarkIndex = url.IndexOf('?');
            
            
if (questionMarkIndex == -1{
                baseUrl 
= url;
                
return;
            }

            baseUrl 
= url.Substring(0, questionMarkIndex);
            
if (questionMarkIndex == url.Length - 1)
                
return;
            
string ps = url.Substring(questionMarkIndex + 1);

            
// 开始分析参数对    
            Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
            MatchCollection mc 
= re.Matches(ps);

            
foreach (Match m in mc) {                
                nvc.Add(m.Result(
"$2"), m.Result("$3"));
            }
        
        }

    }

}





坯子 @ 00:00:00 | 引用 0 | 编辑



评论
发表评论
 姓名: 
 E-mail: 
 地址: