我正在建立一个网站,以使用Sabre的Insta Flight Search API搜索航班数据。仔细阅读他们提供的文档并在API Explorer中测试API之后,一切都可以正常工作,这给了我关于API如何工作的确切思路。

首先,我刚刚创建了一个简单的表单,其中包含2个提供源和目的地IATA代码的选择框,以及2个提供出发日期和返回日期的日期输入框。我一直在寻找解决方案一个多星期,并尝试了各种发现的解决方案,但是每当我收到400错误请求或401未经授权的回复时,就可以找到解决方案。

我还检查了Demo Gallery,但是在编写本文时,还没有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();
    }
}
}


Index.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 with Basic Authentication returns 404 not found for valid URL
c# - Setting Authorization Header of HttpClient
c# - How do I encode and decode a base64 string?
How to post data to specific URL using WebClient in C#


最终编辑

经过数周的可能解决方案的努力,我在响应的格式中找到了答案。 JSON对象在“包含”中将对象包含为字符串,但整个响应也是一个字符串,使内部“包含为\”

但是上个月让我意识到为ASP.NET MVC C#中的开发人员提供Sabre适当的教程的必要,因此我将为其他同行发布博客文章和Nuget包。一旦链接生效,我将分享它们。

最佳答案

以下代码对我来说很好用。

FlightObject f = new FlightObject();
string URL = "https://api.test.sabre.com/v2/shop/flights?origin=JFK&destination=LAX&departuredate=2016-02-04&returndate=2016-02-20&pointofsalecountry=US&passengercount=2";
using (var w = new WebClient())
{
    w.Headers.Add(HttpRequestHeader.Authorization, token_type + " " + access_token);
    string str = w.DownloadString(URL);
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FlightObject));
    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(str)))
    {
        f = (FlightObject)serializer.ReadObject(ms);
    }
}

关于c# - 在ASP.NET MVC C#中的Saber中设置授权 header 和访问数据响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34849280/

10-13 08:27