我正在学习如何使用C#发送请求
我想向航空公司的服务器发送请求,以获取有关航班及其价格的回复。
在邮递员中,我发送以下JSON:
{“ flightList”:[{“ departureStation”:“ KUT”,“ arrivalStation”:“ VIE”,“ from”:“ 2019-03-18”,“ to”:“ 2019-03-31”} ,{“ departureStation”:“ VIE”,“ arrivalStation”:“ KUT”,“从”:“ 2019-04-01”,“到”:“ 2019-05-05”}“],” priceType“:” wdc “,” adultCount“:1,” childCount“:0,” infantCount“:0}
到此URL:
它就像一种魅力。尽管每次尝试使用HttpClient在C#代码中实现它时,每次都会收到404 Not Found错误。
这是我有atm的代码:
static void Main(string[] args)
{
Send("{\"flightList\":[{\"departureStation\":\"KUT\",\"arrivalStation\":\"VIE\",\"from\":\"2019-03-18\",\"to\":\"2019-03-31\"},{\"departureStation\":\"VIE\",\"arrivalStation\":\"KUT\",\"from\":\"2019-04-01\",\"to\":\"2019-05-05\"}],\"priceType\":\"wdc\",\"adultCount\":1,\"childCount\":0,\"infantCount\":0}",
"https://be.wizzair.com/9.7.1/Api/search/timetable/");
}
public static async void Send(string body, string uri)
{
string myJson = body;
var client = new HttpClient();
var response = await client.PostAsync(uri,
new StringContent(myJson, Encoding.UTF8, "application/json"));
var requesturl = response.RequestMessage.Headers;
Console.WriteLine("LOG: " + requesturl.ToString());
var contents = await response.Content.ReadAsStringAsync();
Console.WriteLine(contents);
}
我尝试从文本文件中获取JSON,因为我认为转义字符可能会导致此问题,但并不能解决问题。
不胜感激!