我正在構建一個網站,使用Sabre的Insta Flight Search API來搜索航班數據。在徹底閱讀他們提供的文檔並在API Explorer中測試API之後,所有工作都非常完美,讓我明白API的工作原理。在ASP.NET MVC中設置授權標題和訪問Sabre中的數據響應C#
首先我創建了一個簡單的表單,其中包含兩個提供源和目標IATA代碼的選擇框和兩個日期輸入框,用於指定出發日期和返回日期。我一直在尋找超過一週的解決方案,並嘗試了各種解決方案,但每次我收到400 BAD REQUEST或401 UNAUTHORIZED響應。
我還檢查Demo Gallery但在寫作的時候有對C#沒有樣品,即使是在棧溢出只有78題標記軍刀,只是1標籤的軍刀和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#
最後編輯
與可能的解決方案中掙扎星期後,我發現了個答案e格式化迴應。一個JSON對象在「封閉中包含對象作爲字符串,但是整個響應也是一個字符串,它使內部」封閉到「。但上個月讓我意識到需要爲開發人員開發適合Saber的教程在ASP.NET MVC C#中,所以我將發佈博客文章和其他同行的Nuget包。我將在他們生活後分享鏈接。
也許它是一種愚蠢的問題,但你有沒有試過使用BasicAuthentication和'NetworkCredential'? – Andrew
@Andrew是的,我已經嘗試過基本身份驗證和NetworkCredential也 –
@Downvoter請留下一個反饋describei ng爲什麼你的downvote一年問題的原因,而不是簡單地downopoting –