Yahoo Finance API:远程服务器返回错误:(414)请求URI太长

时间:2013-11-22 09:18:10

标签: c# url webclient yahoo yahoo-finance

我发送请求资助雅虎,如果符号最大为200,我的代码工作正常。如果符号超过200我收到错误“远程服务器返回错误:(414)请求URI太长。“

实际符号超过20000

你能提出一个可能的解决方案吗?

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

1 个答案:

答案 0 :(得分:0)

我根据我计算的符号分隔请求。  以下解决方案对我有用。

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

    }