我正在建立一个网站来使用Sabre 的Insta Flight Search API搜索航班数据。在彻底阅读了他们提供的文档并在API Explorer中测试了 API 之后,一切都运行良好,让我对 API 的工作方式有了确切的了解。
首先,我创建了一个简单的表单,其中包含 2 个提供源和目的地 IATA 代码的选择框,以及 2 个提供出发和返回日期的日期输入框。我一直在寻找解决方案一个多星期,并尝试了我找到的各种解决方案,但每次我收到400 BAD REQUEST或401 UNAUTHORIZED响应。
我还检查了演示库,但在撰写本文时没有 C# 示例,即使在 Stack-Overflow 上也只有 78 个问题标记为 saber,只有 1 个标记为 saber 和 C#
我联系了支持人员,但收到了回复。
请记住,我们不提供代码支持。您能否分享 XML(Sabre)文件?
我知道我错过了一些非常愚蠢的东西,但是在尝试了我能想到的一切之后,我正在向社区寻求帮助。我附上了两个代码文件。
注意:注释代码代表我尝试过的各种方法
控制器代码:
using System;
using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Threading.Tasks;
namespace Sabre_sample_1.Controllers
{
public class HomeController : Controller
{
public async Task<ActionResult> Index()
{
if (Request.HttpMethod == "POST")
{
string Origin = Request.Form["Origin"];
string Destination = Request.Form["Destination"];
DateTime Departure = Convert.ToDateTime(Request.Form["Departure"]);
string departuredatestr = Departure.Year.ToString() + "-" + Departure.Month.ToString() + "-" + Departure.Day.ToString();
DateTime Return = Convert.ToDateTime(Request.Form["Return"]);
string returndatestr = Return.Year.ToString() + "-" + Return.Month.ToString() + "-" + Return.Day.ToString();
WebClient datawebclient = new WebClient();
string url = "https://" + "api.test.sabre.com/v1/shop/flights?origin=" + Origin + "&destination=" + Destination
+ "&departuredate=" + departuredatestr + "&returndate=" + returndatestr + "&onlineitinerariesonly=N"
+ "&limit=10&offset=1&eticketsonly=N&sortby=totalfare&order=asc&sortby2=departuretime&order2=asc"
+ "&pointofsalecountry=US";
string data = string.Empty;
string AccessToken = "*Access Token*";
//datawebclient.Headers.Add("Authorization", "Bearer " + AccessToken);
//datawebclient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + AccessToken);
//datawebclient.Headers.Add(HttpRequestHeader.Authorization, AccessToken);
//data = datawebclient.DownloadString(url);
//HttpClient httpClient = new HttpClient();
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);
//data = await httpClient.GetStringAsync(url);
using (var client = new HttpClient())
{
//url = "https://www.theidentityhub.com/{tenant}/api/identity/v1";
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);
data = await client.GetStringAsync(url);
// Parse JSON response.
}
ViewBag.url = url;
ViewBag.data = data;
//RedirectResult redirectresult = new RedirectResult(url);
}
return View();
}
}
}
索引.cshtml:
<form class="form-horizontal" method="post">
<fieldset>
<legend>Enter Details</legend>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Origin</label>
<div class="col-lg-10">
<select class="form-control" name="Origin" required>
<option></option>
<option value="JFK">John F. Kennedy International Airport</option>
<option value="EZE">Ministro Pistarini</option>
<option value="MIA">Miami International Airport</option>
</select>
</div>
</div>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Destination</label>
<div class="col-lg-10">
<select class="form-control" name="Destination" required>
<option></option>
<option value="JFK">John F. Kennedy International Airport</option>
<option value="EZE">Ministro Pistarini</option>
<option value="MIA">Miami International Airport</option>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Departure</label>
<div class="col-lg-10">
<input type="date" class="form-control" name="Departure"required>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-lg-2 control-label">Return</label>
<div class="col-lg-10">
<input type="date" class="form-control" name="Return" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<input name="endpointName" type="hidden" value="Air Search" class="form-control">
<input name="methodName" type="hidden" value="InstaFlights Search" class="form-control">
<input name="httpMethod" type="hidden" value="GET" class="form-control">
<input name="methodUri" type="hidden" value="/v1/shop/flights" class="form-control">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
<h2>Url</h2>
@ViewBag.url
<hr />
<h2>Data</h2>
@ViewBag.data
其他最接近我的问题但没有解决我的问题的堆栈溢出问题。
- c# - 具有基本身份验证的 Webclient / HttpWebRequest 返回 404 未找到有效 URL
- c# - 设置HttpClient的授权头
- c# - 如何编码和解码 base64 字符串?
- 如何在 C# 中使用 WebClient 将数据发布到特定 URL
最终编辑
在为可能的解决方案苦苦挣扎数周后,我在响应的格式中找到了答案。JSON 对象将对象作为字符串包含在 " 包围中,但整个响应也是一个字符串,它使内部 " 包围到 \"
但是上个月让我意识到需要为 ASP.NET MVC C# 开发人员提供适当的 Sabre 教程,因此我将为其他同行发布博客文章和 Nuget 包。链接上线后,我将分享它们。