c# - 在 C# 中请求 API 信息
问题描述
我正在尝试从 Scopus API(为 python 制作)获取信息,但我使用的是C# mvc(必须)。我的意思是 api Url:“https://api.elsevier.com/”和 api 密钥“3b ...”,我尝试以这种方式提出请求:
private readonly ScopusSearchClient apiClient = new ScopusSearchClient("https://api.elsevier.com/", "3b...");
ScopusSearchClient 指的是:
public ApiClient(string apiUrl, string apiKey)
{
_apiUrl = apiUrl;
_apiKey = apiKey;
_httpClient = new HttpClient
{
BaseAddress = new Uri(_apiUrl)
};
}
(ScopusSearchClient 链接到 ApiClient 允许我这样做)。
然后我尝试通过以下方式执行实际请求:
var scopusSearchResult = await apiClient.GetAsync<SearchResults<Scopus>>("content/search/scopus", "query=AF-ID(Harvard Medical School 3000604");
哪个是功能:
public async Task<T> GetAsync<T>(string endpoint, string parameters = null) where T : HttpStatusResource
{
HttpResponseMessage httpResponseMessage = await _httpClient.GetAsync(Utilities.GenerateRequestUri(_apiUrl, endpoint, _apiKey, parameters));
string strContent = await httpResponseMessage.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<T>(strContent);
result.IsSuccessStatusCode = httpResponseMessage.IsSuccessStatusCode;
result.StatusCode = httpResponseMessage.StatusCode;
return result;
}
问题是,当我连接时,我收到错误:
连接尝试失败,因为连接方超过一定时间没有正确响应或建立连接失败,因为连接主机没有响应 104.XX.XX.XX:XXX
它来自这条线
var scopusSearchResult = await apiClient.GetAsync<SearchResults<Scopus>>("content/search/scopus", "query=AF-ID(Harvard Medical School 3000604");
在控制器中。
有人知道发生了什么吗?非常感谢,高蒂尔
解决方案
最后是防火墙的问题,阻止了一些 IP,包括我们正在查询以从 API 获取信息的 URL 之一。通过防火墙解除封锁!
推荐阅读
- flutter - 为什么颤振桌面嵌入会在构建时引发错误?
- javascript - React 应用程序:Jest 遇到了意外的令牌
- buildroot - 如何使用 Buildroot 更改内核版本字符串
- android - Kotlin Json 解析 MVVM
- php - php中带有XPath的HTML表格
- java - MB_LEN_MAX 的 Java 等价物是什么?
- python-3.x - Python 3 中的意外行为:列表内容在打印后消失
- python - python 内置的 __exit__ 参数类型是什么?
- sql - sql中的延迟向上递归查询
- android - 未选择的选项卡底部栏未使用 Xamarin.Forms Shell 显示(仅限 Android)