2011-12-27 162 views
2

我正在使用這些代碼從服務器下載文件。如何點擊網頁中的按鈕

服務器從那裏我需要下載文件的鏈接是:

http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx

 var forms = new NameValueCollection(); 

     forms["__EVENTARGUMENT"] = ""; 
     forms["__VIEWSTATE"] = ExtractVariable(s, "__VIEWSTATE"); 
     forms["mTbdate"] = "12/22/2011"; 
     forms["__EVENTVALIDATION"] = __EVENTVALIDATION; 
     forms["mImgBtnGo"] = "?"; 
     forms["__EVENTTARGET"] = "btnLink_Excel"; 

     webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); 

     var responseData = webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms); 
     System.IO.File.WriteAllBytes(@"c:\11152011.csv", responseData); 

其下載在文本框中是默認的網站現在定日期的文件。

我需要點擊一個名爲mImgBtnGo之前按鈕,下載在mTbdate給定日期

的文件。

我不知道該怎麼做才能點擊名爲mImgBtnGo的按鈕。

我應該使用fiddler我想這是你想要的這裏寫

   forms["mImgBtnGo"] = "?"; 

回答

4

class Program 
    { 

     static string Extract(string s, string tag) 
     { 
      var startTag = String.Format("id=\"{0}\" value=\"", tag); 
      var eaPos = s.IndexOf(startTag) + startTag.Length ; 
      var eaPosLast = s.IndexOf('"', eaPos); 
      return s.Substring(eaPos, eaPosLast-eaPos); 
     } 

     static void Main(string[] args) 
     { 

      WebClient webClient = new WebClient(); 

      var firstResponse = webClient.DownloadString(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx"); 

      var forms = new NameValueCollection(); 
      forms["__EVENTARGUMENT"] = ""; 
      forms["__VIEWSTATE"] = Extract(firstResponse, "__VIEWSTATE"); 
      forms["mTbdate"] = "12/22/2011"; 
      forms["__EVENTVALIDATION"] = Extract(firstResponse, "__EVENTVALIDATION"); 
      forms["mImgBtnGo.x"] = "10"; 
      forms["mImgBtnGo.y"] = "10"; 
      forms["ScriptManager1"] = "MupdPnl|mImgBtnGo"; 
      // forms["__EVENTTARGET"] = "btnLink_Excel"; 
      webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); 

      String secondResponse = UTF8Encoding.UTF8.GetString(
       webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms) 
      ); 

      forms = new NameValueCollection();   
      forms["__EVENTARGUMENT"] = ""; 
      forms["__VIEWSTATE"] = Extract(secondResponse, "__VIEWSTATE");   
      forms["mTbdate"] = "12/22/2011"; 
      forms["__EVENTVALIDATION"] = Extract(secondResponse, "__EVENTVALIDATION");   
      // forms["mImgBtnGo"] = "?";   
      forms["__EVENTTARGET"] = "btnLink_Excel";   
      webClient.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");   
      var responseData = webClient.UploadValues(@"http://www.mcxindia.com/sitepages/BhavCopyDateWise.aspx", "POST", forms);   
      System.IO.File.WriteAllBytes(@"c:\prj\11152011.csv", responseData);  
      } 

    } 
+0

沒有它的下載頁面 – 2011-12-27 12:36:16

+0

哦源,你要下載的Excel工作表!你可以把mImgBtnGo都放在一起。它在POST中不需要。 – rene 2011-12-27 12:39:17

+0

但它的下載12/26/2011日期只有不是以前的日期 – 2011-12-27 12:46:00