C#在webclient中传递GET参数

时间:2015-04-14 13:54:14

标签: c# asp.net ajax screen-scraping webclient

我试图在点击按钮后捕获返回html。我已经使用fiddler来查看用户单击按钮时发送的请求。以下是标题信息。

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

我有以下代码无效。有人可以指出我正确的解决方案吗?

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

1 个答案:

答案 0 :(得分:1)

您必须填写标题的足够信息。 它适用于此代码:

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