如何解析Web链接

废话不多说,直接上例子和代码:

对于链接strURL = http://local/Fun=cmd_popupwindow&p1=http://www.hao123.com

根据分隔符“&”和“=”可以解析出函数名字 cmd_popupwindow 和参数链接 http://www.hao123.com

注:以下代码不管链接参数是多少,只要分隔符是&都可以。

 1 void UMSFrameBottom::AnalasisURL(const CString& strUrl, TCHAR csplit, CStringArray& aryCString)
 2 {
 3     aryCString.RemoveAll();
 4     int npos = 0;
 5     int nNewpos = -1;
 6     int nUrlLen = strUrl.GetLength();
 7     int ncsplit = 1;
 8 
 9     if(nUrlLen<0) return ;
10     std::vector<int> aryPosition;
11     nNewpos = strUrl.Find(csplit, 0);
12 
13     if(nNewpos<0) 
14     {
15         aryCString.Add(strUrl);
16         return ;
17     }
18     while(nNewpos>npos)
19     {
20         aryPosition.push_back(nNewpos);
21         npos = nNewpos;
22         nNewpos = strUrl.Find(csplit, npos+1);
23     }
24     CString strSub1,strSub;
25     for(int i=0; i<=aryPosition.size(); ++i)
26     {
27         strSub.Empty();
28         if(i == 0)
29         {
30             strSub1 = strUrl.Mid(i, aryPosition[i]);    
31             int subPos = strSub1.Find(_T("="), 0);
32             strSub = strSub1.Right(strSub1.GetLength()-subPos-1);
33         }
34         else
35         {
36             if(i == aryPosition.size())
37             {
38                 strSub1 = strUrl.Mid(aryPosition[i-1]+ncsplit);
39                 int subPos = strSub1.Find(_T("="), 0);
40                 strSub = strSub1.Right(strSub1.GetLength()-subPos-1);
41             }
42             else if(i>0)
43             {
44                 strSub1 = strUrl.Mid(aryPosition[i-1]+ncsplit, aryPosition[i]-aryPosition[i-1]-ncsplit);
45                 int subPos = strSub1.Find(_T("="), 0);
46                 strSub = strSub1.Right(strSub1.GetLength()-subPos-1);
47             }            
48         }
49 
50         if(strSub.IsEmpty())
51             continue;
52 
53         aryCString.Add(strSub);
54         
55     }
56 
57 
58 }

这样得到CStringArry数组aryCString将得到函数名和参数链接。。。

原文地址:https://www.cnblogs.com/yangxx-1990/p/4882223.html