如何单击网页中的按钮

时间:2011-12-27 12:06:31

标签: c# winforms webclient webpage

我正在使用这些代码从服务器下载文件。

我需要下载文件的服务器的链接是:

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的按钮。

我应该在这里写什么

              forms["mImgBtnGo"] = "?";

1 个答案:

答案 0 :(得分:4)

使用fiddler我认为这就是你想要的:

 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);     
            }

     }