2010-11-08 57 views
0

我想使用屏幕抓取登錄網站,我有困難。C#屏幕抓取處理使用POST的登錄控制

private static string GetWebRequestContent(Uri uri) 
    { 
     string sid = String.Empty; 

     string userName = "username"; 
     string password = "password"; 
     string postData = string.Format("FormName=Login&Login={0}&Password={1}&FormAction=login&ret_page=%2FDefault.asp&querystring=",userName,password); 
     byte[] postBytes = Encoding.UTF8.GetBytes(postData); 


     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri); 
     req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E; MS-RTC LM 8)"; 
     req.KeepAlive = false; 
     req.Proxy = new WebProxy(IPAddress,PORT); 
     req.Method = "POST"; 
     req.Host = host; 
     req.Referer = referer; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.ContentLength = postBytes.Length; 

     StreamWriter requestwriter = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII); 
     requestwriter.Write(postData); 
     requestwriter.Close(); 

     try 
     { 
      using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) 
      { 
       using (StreamReader sr = new StreamReader(res.GetResponseStream())) 
       { 
        sid = sr.ReadToEnd().Trim(); 
       } 
      } 
     } 
     catch (WebException e) 
     { 
      Console.WriteLine(uri.ToString()); 
      Console.WriteLine(e.Message); 
     } 

     return sid; 
    } 

我得到的所有回報是登錄表單,它從來沒有登錄我。任何想法?

回答

0

嘗試監視在瀏覽器中發送了什麼請求,然後在腳本中複製該請求。有各種各樣的Firefox擴展可以幫助你做到這一點,例如TamperData,FirebugLiveHttp