首页 > 解决方案 > 为什么我的 webrequest 行需要更新?(错误(426)需要升级)

问题描述

我正在WebRequestVisual Studio 中创建一个 WCF 服务应用程序。这段代码就像 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..... 

标签: c#api

解决方案


我终于找到了答案。在您放置 的代码行中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';

推荐阅读