2015-04-24 74 views
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(); 
    } 

回答

1

一般。首先在像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 
    } 
}; 

如果你没有任何限制,我建议你使用制作Web请求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对其进行反序列化。

相关问题