I am a novice in programming. I have a project where I have to get data from a website (I ll be posting the website below). But it happens that I first have to select a date and press a 'Go' button and then click on another Button/Link 'View in Excel' to download this data. I cannot find a way to first click the 'Go' button after selecting my required date and then click the second button 'View in Excel' through my c# code.
Any help will be appreciated.

PS: You can check the link for yourselves http://www.mcxindia.com/sitepages/BhavCopyDateWiseArchive.aspx

private void Form1_Load(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();

            byte[] b = webClient.DownloadData("http://www.mcxindia.com/sitepages/BhavCopyDatewise.aspx");

            string s = System.Text.Encoding.UTF8.GetString(b);

            var __EVENTVALIDATION = ExtractVariable(s, "__EVENTVALIDATION");

            //__EVENTVALIDATION.Dump();

            var forms = new NameValueCollection();

            forms["__EVENTTARGET"] = "btnLink_Excel";
            forms["__EVENTARGUMENT"] = "";
            forms["__VIEWSTATE"] = ExtractVariable(s, "__VIEWSTATE");
            forms["mTbdate"] = "01%2F15%2F2013";
            forms["__EVENTVALIDATION"] = __EVENTVALIDATION;

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

        private static string ExtractVariable(string s, string valueName)
        {   
            string tokenStart = valueName + "\" value=\"";
            string tokenEnd = "\" />";

            int start = s.IndexOf(tokenStart) + tokenStart.Length;
            int length = s.IndexOf(tokenEnd, start) - start;
            return s.Substring(start, length);
        }

The above code gives me the file only for the last available date. I am not able to get it for the date I want.

Recommended Answers

All 2 Replies

What is the last available date? At the moment you're requesting the hard-coded date of the 15th Jan, 2013. You realise that this site only stores data up to December 2012?

The date doesnt matter. Whatever date I put it gives me the last available data.

PS: I found a solution to this question. Pls Click Here for solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.