c# - 为什么我的 webrequest 行需要更新?(错误(426)需要升级)
问题描述
我正在WebRequest
Visual 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.....
解决方案
我终于找到了答案。在您放置 的代码行中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';
推荐阅读
- javascript - 在 .vue 文件中包含 JS 文件
- sql - 想要为 SQLite 数据库做一个重复的任务
- c# - 使用 Visual Studio 分析器分析争用时如何忽略长时间运行的信号等待?
- java - 处理 Java 中的无效输入
- ios - Core Haptics 和 Core Bluetooth 会干扰吗?
- html - 如何缩小手机屏幕上的logo?
- microservices - 设计一个流数据管道,用于消费从多个服务到 Hadoop 的事件
- python - 类型错误:+ 不支持的操作数类型:“WSGIRequest”和“str”
- c# - 如何反序列化 XML 属性?
- java - 如何替换字符串中的字母 'a' 并递归地用另外两个 a 替换它?