QPX Express Airfare API return The remote server returned an error: (400) Bad Request
QPX Express Airfare API return The remote server returned an error: (400) Bad Request
我正在使用 QPX Express Airfare API 获取 JSON 格式的机票,所以我通过 google 开发者控制台启用了 QPX Express Airfare API 然后生成API 相应的键。
我通过 QPX Express 演示测试我的 api 密钥和 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
我指的是这个 link :
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();
}
通常当您从 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 for JSON serialization and 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 反序列化它。
我正在使用 QPX Express Airfare API 获取 JSON 格式的机票,所以我通过 google 开发者控制台启用了 QPX Express Airfare API 然后生成API 相应的键。
我通过 QPX Express 演示测试我的 api 密钥和 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
我指的是这个 link :
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();
}
通常当您从 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 for JSON serialization and 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 反序列化它。