为什么我的webrequest行需要更新? (错误(426)需要升级)

时间:2019-10-21 02:04:36

标签: c# api

我正在Visual Studio中的WCF服务应用程序上制作WebRequest。这段代码像2天前一样工作,现在我收到此错误。

我尝试更新api密钥。

public List<string> GetData(string topic)
{
        // formats the url properly
        string beginning = "https://newsapi.org/v2/everything?q=";
        string ending = "&from=2019-09-14&sortBy=publishedAt&apiKey=ce1ad5bceee84d958dd9ca5bc72488a";
        string mid = topic.Replace(" ", "+");
        string url = beginning + mid + ending;

        // to hold the article urls
        List<string> all_urls = new List<string>();

        using (var webClient = new WebClient())
        {
            String rawJSON = webClient.DownloadString(url);
            var newsAnchor = JsonConvert.DeserializeObject< RootObject >(rawJSON);
            foreach (Article urs in newsAnchor.articles)
            {
                all_urls.Add(urs.url); 
            }
        }

        return all_urls;
}

编译器返回此错误:

  

System.Net.WebException:'远程服务器返回错误:(426)需要升级。'

包含

的行
string rawJSON = webClient..... 

2 个答案:

答案 0 :(得分:3)

我终于找到了答案。在放置apiKey的代码行中,将日期更改为不早于今天的一个月。例如,由于今天是2020年5月12日,因此代码中的日期不应早于2020年4月12日。我希望这会有所帮助。

from
    'http://newsapi.org/v2/everything?q=' +
         searchTerm + '&from= '''2020-04- 10'''&sortBy=publishedAt&apiKey=967d749e88694f2088bd47900f907bc4';

to

     'http://newsapi.org/v2/everything?q=' +
          searchTerm + '&from= '''2020-04-12''' &sortBy=publishedAt&apiKey=967d749e88694f2088bd47900f907bc4';

答案 1 :(得分:2)

如果检查返回的内容,就会看到问题。

这就是我从

获得的
https://newsapi.org/v2/everything?q=xxx&from=2019-09-14&sortBy=publishedAt&apiKey=my_temp_key
status  "error"
code    "parameterInvalid"
message "You are trying to request results too far in the past. Your plan permits you to request articles as far back as 2019-09-20, but you have requested 2019-09-14. To extend this please upgrade to a paid plan."
相关问题