我正在尝试向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!");
}
}
}