Question

I am sending request to finance yahoo and my code below is working fine if the symbols are maximum 200. If the symbols are more than 200 i receive an error "The remote server returned an error: (414) Request URI Too Long."

The actual symbols are more than 20000

Can you propose a possible solution?

string yahooQuoteUrl = "http://finance.yahoo.com/d/quotes.csv?s=";
string yahooParameters = "&f=sl1d1yxn";
string Url ="";
SqlConnection sqlConnection = new SqlConnection(con1);
SqlCommand sqlCommand = new SqlCommand("SELECT Symbol FROM PM_Securities",sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView2.DataSource = reader;
GridView2.DataBind();
string symbols= GridView2.Rows[0].Cells[0].Text;

string csvData;
    using (WebClient web = new WebClient())
    {
        Url = yahooQuoteUrl + symbols+ yahooParameters;
        csvData = web.DownloadString(Url);
    }


    List<Price> prices = Parse(csvData);
    GridView1.DataSource = prices;
    GridView1.DataBind();
Was it helpful?

Solution

I separate the requests depending on the symbols that i have count. The below solution is working good for me.

  if (count <= 200) {

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText200 + yahooParameters;
            csvData200 = web.DownloadString(Url);
        }

        csvData = csvData200;
        List<Price> prices = Parse(csvData);
        GridView1.DataSource = prices;
        GridView1.DataBind();

    }


    if (count>=201 & count<=400)
    {

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText200 + yahooParameters;
            csvData200 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText400 + yahooParameters;
            csvData400 = web.DownloadString(Url);
        }

        csvData = csvData200 + csvData400;
        List<Price> prices = Parse(csvData);
        GridView1.DataSource = prices;
        GridView1.DataBind();
    }


    if (count >= 401 & count <= 600)
    {

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText200 + yahooParameters;
            csvData200 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText400 + yahooParameters;
            csvData400 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText600 + yahooParameters;
            csvData600 = web.DownloadString(Url);
        }

        csvData = csvData200 + csvData400 + csvData600;
        List<Price> prices = Parse(csvData);
        GridView1.DataSource = prices;
        GridView1.DataBind();

    }



    if (count >= 601 & count <= 800)
    {

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText200 + yahooParameters;
            csvData200 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText400 + yahooParameters;
            csvData400 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText600 + yahooParameters;
            csvData600 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText800 + yahooParameters;
            csvData800 = web.DownloadString(Url);
        }

        csvData = csvData200 + csvData400 + csvData600 + csvData800;
        List<Price> prices = Parse(csvData);
        GridView1.DataSource = prices;
        GridView1.DataBind();

    }

    if (count >= 801 & count <= 1000)
    {

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText200 + yahooParameters;
            csvData200 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText400 + yahooParameters;
            csvData400 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText600 + yahooParameters;
            csvData600 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText800 + yahooParameters;
            csvData800 = web.DownloadString(Url);
        }

        using (WebClient web = new WebClient())
        {
            Url = yahooQuoteUrl + cellText1000 + yahooParameters;
            csvData1000 = web.DownloadString(Url);
        }

        csvData = csvData200 + csvData400 + csvData600 + csvData800 + csvData1000;
        List<Price> prices = Parse(csvData);
        GridView1.DataSource = prices;
        GridView1.DataBind();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top