简体   繁体   中英

C# Passing GET parameter in the webclient

I am trying to capture the return html after a button click. I've used fiddler to see what request gets sent when the user clicks a button. Below is the header information.

GET http://www.nseindia.com/products/dynaContent/equities/equities/bulkdeals.jsp?symbol=&segmentLink=13&symbolCount=&dateRange=day&fromDate=&toDate=&dataType=DEALS HTTP/1.1
    Host: www.nseindia.com
    Proxy-Connection: keep-alive
    Accept: */*
    X-Requested-With: XMLHttpRequest
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
    Referer: http://www.nseindia.com/products/content/equities/equities/bulk.htm
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8

I have the below code which is not working. Can someone please point me to the correct solution ?

  var nseBulkDealsUrl = @"http://www.nseindia.com/products/dynaContent/equities/equities/bulkdeals.jsp?symbol=&segmentLink=13&symbolCount=&dateRange=day&fromDate=&toDate=&dataType=DEALS";

    var client = new WebClient();
    client.Encoding = Encoding.UTF8;

    var values = new NameValueCollection();
    values.Add("Referer", "http://www.nseindia.com/products/content/equities/equities/bulk.htm");
    values.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
    values.Add("X-Requested-With", "XMLHttpRequest");

    client.Headers.Add(values);
    client.Proxy = WebRequest.DefaultWebProxy; 

    var htmlPageSource = client.DownloadString(nseBulkDealsUrl);

You have to fill enough information for headers. It works with this code:

  var nseBulkDealsUrl = @"http://www.nseindia.com/products/dynaContent/equities/equities/bulkdeals.jsp?symbol=&segmentLink=13&symbolCount=&dateRange=day&fromDate=&toDate=&dataType=DEALS";
        var client = new WebClient();
        client.Encoding = Encoding.UTF8;

        var values = new NameValueCollection();
        values.Add("Referer", "http://www.nseindia.com/products/content/equities/equities/bulk.htm");
        values.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
        values.Add("X-Requested-With", "XMLHttpRequest");
        values.Add("Accept", "*/*");
        //values.Add("Accept-Encoding", "gzip, deflate, sdch");
        values.Add("Accept-Language", "en-US,en;q=0.8");
        client.Proxy = WebRequest.DefaultWebProxy;
        //client.UploadValues(nseBulkDealsUrl, "GET", values);
        client.Headers.Add(values);
        var htmlPageSource = client.DownloadString(nseBulkDealsUrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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