比如说csdn上的用户留言,随时更新的,数据量很大,我想把这些数据下载到本地,然后进行操作,但csdn的数据库不可能暴露给我,我用什么方法,可以把这些数据下载到本地?

解决方案 »

  1.   

    模拟登录,利用httpWebRequest
      

  2.   

    public  string getHTMLByUrlCook(string url,ref System.Net.CookieContainer cook,string sMethod,string Param,bool bAutoRedirect,System.Text.Encoding ecode)
    {
    sMethod = sMethod.ToUpper();
    sMethod = sMethod!="POST"?"GET":sMethod;
    string res ="";
    HttpWebRequest re =(HttpWebRequest)HttpWebRequest.Create(url);
    re.CookieContainer = cook; // attach the cook object 
    re.Method = sMethod;
    re.AllowAutoRedirect = bAutoRedirect;
    re.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)";


    if(Public.Session.bUsePox) //利用代理
    {
    int Port = 80;
    if(Public.IsInt(this.Session.PoxPort))
    {
    Port = Convert.ToInt32(this.Session.PoxPort);
    }
    WebProxy pox = new WebProxy(this.Session.PoxIp,Port);
    pox = (WebProxy)re.Proxy;
    }


    //re.Referer="http://expert.csdn.net/Expert/topic/2839/2839298.xml?temp=.2714197";
    //re.ClientCertificates = new System.Security.Cryptography.X509Certificates.X509CertificateCollection();
    //re.ClientCertificates = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile(); //re.Timeout = 2000;
    re.Referer = url;
    if (sMethod =="POST")  // Post data to Server
    {
    re.ContentType="application/x-www-form-urlencoded";
    Byte[] b = this.getByteFromString(Param,ecode);
    re.ContentLength = b.Length;
    try
    {
    Stream oSRe = re.GetRequestStream();
    oSRe.Write(b,0,b.Length);
    oSRe.Close();
    oSRe = null;
    }
    catch(Exception )
    {
    re = null;
    return "-1";
    }
    }

    HttpWebResponse rep = null;
    Stream oResponseStream = null;
    StreamReader oSReader = null;
    try
    {
    rep=(HttpWebResponse)re.GetResponse();

    oResponseStream = rep.GetResponseStream();
    oSReader  = new StreamReader(oResponseStream,ecode);
    res =oSReader.ReadToEnd();
    }
    catch (System.Net.WebException  e)
    {
    //res ="-1";

    res = e.ToString();
    } if (rep!=null) 
    {
    rep.Close();
    rep = null;
    }
    if(oResponseStream!= null)
    {
    oResponseStream.Close();
    oResponseStream = null;
    }

    if(oSReader!=null)
    {
    oSReader.Close();
    oSReader = null;
    }
    re = null; return res;

    }
      

  3.   

    如果说要csdn这个页面上的数据,往方法里传递哪些值啊?可否给我一个使用这个方法的代码?
      

  4.   

    传哪些值,你要分析目录网页的<form中的元素。具体的不多说了,也不要拿csdn做例子。
      

  5.   

    private void getHtml()
    {
    getHTMLByUrlCook(string url,ref System.Net.CookieContainer cook,string sMethod,string Param,bool bAutoRedirect,System.Text.Encoding ecode)}
      

  6.   


    <html>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <link rel="stylesheet" href="../all.css" type="text/css">
    <link rel="stylesheet" href="../VIP.css" type="text/css">
    </head>
    <body bgcolor="#FFFFFF" text="#000000" leftmargin="5" topmargin="0" marginwidth="0" marginheight="0" background="../images/bg00.gif">
                <table width="75%" border="1" cellspacing="1" cellpadding="2" align="center" bordercolor="#CCCCCC">
                  <tr> 
                    <td> 
                      <div align="center"><b>姓名或公司名</b></div>
                    </td>
                    <td> 
                      <div align="center"><b>统一号码</b></div>
                    </td>
                  </tr>
                  
                  <tr> 
                    <td> 
                      <div align="center">需要下载的数据</div>
                    </td>
                    <td> 
                      <div align="center">需要下载的数据</div>
                    </td>
                  </tr>
           </table>
    </body>
    </html>