0
我使用QPX Express Airfare API来获取JSON格式的航空公司票价,因此我通过Google开发者控制台启用QPX Express Airfare API,然后生成API相应的关键。QPX Express Airfare API返回远程服务器返回错误:(400)错误的请求
我测试我的api密钥&通过QPX Express演示的json请求。它的工作正常,但我的代码中有异常。我在下面提到异常。
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Additional information: The remote server returned an error: (400) Bad Request
我指的是这个链接:
https://developers.google.com/qpx-express/v1/trips/search
代码:当你从API 400状态码,这意味着你的要求没有得到很好的形成
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyBuCQkshTNNDbMidIPzzLofG8Q-izi1PNA");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//string json = "{\"user\":\"test\"," +
// "\"password\":\"bla\"}";
string json = new JavaScriptSerializer().Serialize(new
{
origin = "LAS",
destination="LAX",
date="2015-04-30",
adultCount="1",
infantInLapCount="0",
infantInSeatCount="0",
childCount="0",
seniorCount="0",
solutions="20",
refundable = "false"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}