首页 > 解决方案 > 在 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");

在控制器中。

有人知道发生了什么吗?非常感谢,高蒂尔

标签: c#apihttpscopus

解决方案


最后是防火墙的问题,阻止了一些 IP,包括我们正在查询以从 API 获取信息的 URL 之一。通过防火墙解除封锁!


推荐阅读