等待client.GetAsync返回NULL

时间:2016-06-14 18:05:10

标签: c# xml

我正在尝试向API发送HttpClient请求,但我的回复内容始终为NULL。当我尝试通过Postman发送完全相同的GET请求时,我发现确实正在返回数据。此外,当我使用Fiddler检查来自我的应用程序的请求时,我也看到返回了XML数据。我在VS中设置断点时为什么会看到NULL的任何想法?

尽管GET请求中返回了实际内容,但下面代码中的response变量和xml变量都设置为NULL

static void Main(string[] args)
{
    RunAsync().Wait();
}
static async Task RunAsync()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(URL);

        var response = await client.GetAsync(urlParameters);
        if (response.IsSuccessStatusCode)
        {
            var xml = await response.Content.ReadAsStringAsync();
            var ds = new XmlSerializer(typeof(trainData), new XmlRootAttribute("ctatt"));
            using (StringReader sr = new StringReader(xml))
            {
                using (XmlReader xr = XmlReader.Create(sr))
                {
                    var trainData = (trainData)ds.Deserialize(xr);
                    Console.WriteLine("Station Name: {0}\nRoute Name: {1}\nArrival Time: {2}", trainData.stationName, trainData.routeName, trainData.arrTime);
                }
            }
        }
        else
        {
            Console.WriteLine("There was an error!");
        }
    }
}

0 个答案:

没有答案