我正在使用QPX Express Airfare API以获得JSON格式的航空公司票价,因此我通过谷歌开发者控制台启用了QPX Express Airfare API,然后相应地生成API密钥。
我测试了我的api键& json请求通过QPX Express演示。它工作正常,但我的代码中有异常。我在下面提到了例外情况。
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
代码:
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();
}
答案 0 :(得分:1)
通常当您从API获得400状态代码时,这意味着您的请求格式不正确。在像Fiddler这样的工具中首次执行请求总是一个好主意。您的样本将得到回复:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Invalid inputs: received empty request."
}
],
"code": 400,
"message": "Invalid inputs: received empty request."
}
}
您始终可以使用匿名对象,但您也应该考虑创建模型。使用json2csharp.com等服务非常简单。所以你的请求模型可以是:
public class Passengers
{
public string kind { get; set; }
public int? adultCount { get; set; }
public int? childCount { get; set; }
public int? infantInLapCount { get; set; }
public int? infantInSeatCount { get; set; }
public int? seniorCount { get; set; }
}
public class PermittedDepartureTime
{
public string kind { get; set; }
public string earliestTime { get; set; }
public string latestTime { get; set; }
}
public class Slouse
{
public string kind { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string date { get; set; }
public int? maxStops { get; set; }
public int? maxConnectionDuration { get; set; }
public string preferredCabin { get; set; }
public PermittedDepartureTime permittedDepartureTime { get; set; }
public List<string> permittedCarrier { get; set; }
public string alliance { get; set; }
public List<string> prohibitedCarrier { get; set; }
}
public class Request
{
public Passengers passengers { get; set; }
public List<Slouse> slice { get; set; }
public string maxPrice { get; set; }
public string saleCountry { get; set; }
public bool? refundable { get; set; }
public int? solutions { get; set; }
}
public class RootObject
{
public Request request { get; set; }
}
我只将所有值类型更改为可空类型。现在您可以创建有效的请求:
var request = new RootObject
{
request = new Request
{
passengers = new Passengers
{
adultCount = 1
},
slice = new List<Slouse>
{
new Slouse
{
origin = "LAS",
destination = "LAX",
date = "2015-04-30"
}
},
solutions = 20,
refundable = false
}
};
如果您没有任何限制,我建议您使用JSON.NET进行JSON序列化,使用HttpClient进行网络请求:
string requestJson = JsonConvert.SerializeObject(request,
Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
using (var httpClient = new HttpClient())
{
var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
var response = httpClient.PostAsync(url, content).Result;
var res = response.Content.ReadAsStringAsync().Result;
}
您可能还应该使用JSON.NET创建响应模型并对其进行反序列化。