卷曲命令到C#web rest api请求

时间:2017-04-01 19:40:21

标签: c# rest curl

我有以下cURL命令可以正常工作以获取我需要的数据,并且我试图从我的C#代码中调用此API。

curl "https://api.propublica.org/congress/v1/members/senate/RI/current.json" -   H "X-API-Key: <MyAPIKey>"

我在C#中编写了以下代码来尝试调用此API。

class Program
{
    static HttpClient client = new HttpClient(); 

    static void Main()
    {
        var listing = GetCongressmanByState("IL");
        Console.ReadLine();
    }

    static async Task<List<Congressman>> GetCongressmanByState(string State)
    {
        string apiurl = "https://api.propublica.org/congress/v1/members/senate/" + State + "/current.json";
        string apikey= "<MyAPIKey>";
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-API-Key", apikey);
        HttpResponseMessage response = await client.GetAsync(apiurl);
        if(response.IsSuccessStatusCode)
        {
            string result = await response.Content.ReadAsStringAsync();
            var rootResult = JsonConvert.DeserializeObject<RootObject>(result);
            return rootResult.congressmen;
        }
        else
        {
            return null;
        }


    }

}

尝试逐步执行GetCongressmanByState,程序似乎跳过我的if语句并直接回到main函数。

当我在console.readline()上使用断点运行时,这就是列表中的内容: listing contents

我的代码有问题吗?

0 个答案:

没有答案