4 Replies - 501 Views - Last Post: 06 February 2013 - 07:01 AM Rate Topic: -----

#1 Horsie05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 13

Getting data from .aspx webpage by two button clicks c#.net

Posted 05 February 2013 - 11:04 PM

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....iseArchive.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.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting data from .aspx webpage by two button clicks c#.net

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Getting data from .aspx webpage by two button clicks c#.net

Posted 06 February 2013 - 05:23 AM

So you're trying to create a bot to manipulate the site just as if it were a human operator? If this is your site why don't you just query the database directly?
Was This Post Helpful? 1
  • +
  • -

#3 Horsie05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 13

Re: Getting data from .aspx webpage by two button clicks c#.net

Posted 06 February 2013 - 05:51 AM

View PosttlhIn`toq, on 06 February 2013 - 05:23 AM, said:

So you're trying to create a bot to manipulate the site just as if it were a human operator? If this is your site why don't you just query the database directly?



This is not my website. and i want to get the data from it through my code.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: Getting data from .aspx webpage by two button clicks c#.net

Posted 06 February 2013 - 06:18 AM

Ah... Got it... Best of luck with your bot.
Was This Post Helpful? 1
  • +
  • -

#5 Horsie05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 13

Re: Getting data from .aspx webpage by two button clicks c#.net

Posted 06 February 2013 - 07:01 AM

My bot seems to have started moving :)
Click here for solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1