简体   繁体   English

通过两次单击从.aspx网页获取数据c#.net

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

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. 但是碰巧我首先必须选择一个日期并按下“开始”按钮,然后单击另一个按钮/链接“在Excel中查看”以下载此数据。 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. 我找不到一种方法,可以在选择所需日期后先单击“开始”按钮,然后再通过c#代码单击第二个按钮“在Excel中查看”。 Any help will be appreciated. 任何帮助将不胜感激。

PS: You can check the link for yourselves http://www.mcxindia.com/sitepages/BhavCopyDateWiseArchive.aspx PS:您可以自行检查链接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. 我无法在我想要的日期得到它。

Seems like you need to made a HTTP Post request with one post data (date). 似乎您需要使用一个发布数据(日期)发出HTTP Post请求。 This should be easy enough. 这应该很容易。 Have a look at this question and see if it helps you. 看看这个问题,看看是否有帮助。 Send HTTP Post Request through ASP.net 通过ASP.net发送HTTP发布请求

I found a solution to this question. 我找到了解决这个问题的方法。 pls see here 请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM