Asp.net 静态页面生成(3)----动态页生成之 生成爬虫

根据前面讲解的生成方法,需要访问动态页面戴上 Create=true 参数就可以生成了。但是一个一个页面的访问是不是有点累啊
那我们就可以在生成管理器里面写个爬虫去爬遍每一个页面就可以达到生成的目的了。
由于采用http访问生成,爬虫可以在部署在任何位置,实例代码如下

public class Creater
{
    
public Creater(string serverName)
    
{
        
this._ServerName = serverName;
    }

    
private string _ServerName;
    
public string ServerName
    
{
        
get
        
{
            
return _ServerName;
        }

    }

    
public int CreateArticlePage()
    
{
        
string sqlStr = "SELECT ArticelId FROM Article";
        DataSet ds 
= DataBase.ExecuteDataset(sqlStr);
        System.Collections.Generic.List
<string> urls = new System.Collections.Generic.List<string>();
        
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        
{
            
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            
{
                urls.Add(
"http://" + ServerName + "/Article/Show.aspx?ArticleId=" + ds.Tables[0].Rows[i].ToString());
            }

        }

        
return CreateHtml(urls);
    }

    
private int CreateHtml(System.Collections.Generic.List<string> urls)
    
{
        
int result = 0;
        System.Net.WebClient client 
= new System.Net.WebClient();
        
for (int i = 0; i < urls.Count; i++)
        
{
            System.IO.Stream stream 
= client.OpenRead(urls[i]);
            System.IO.StreamReader sr 
= new System.IO.StreamReader(stream);
            result 
+= int.Parse(sr.ReadToEnd());
        }

        
return result;
    }

}
posted @ 2008-03-23 21:54  陈大杰  阅读(993)  评论(0编辑  收藏  举报