Preface:
Execution code:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CONTROL_UI
{
public partial class RunningConfig : Form
{
public RunningConfig()
{
InitializeComponent();
var client = new RestClient("CLIENT_URL");
var loginRequest = new RestRequest("api/login", Method.POST);
loginRequest.RequestFormat = DataFormat.Json;
loginRequest.AddHeader("Content-type", "application/json");
loginRequest.AddJsonBody(new LoginForm() { username = "admin", password = "", @override = true });
var loginRespone = client.Execute(loginRequest);
JObject sessionInfo = JObject.Parse(loginRespone.Content);
var sessionID = sessionInfo["sid"];
var seatAvailRequest = new RestRequest("api/seats", Method.GET);
seatAvailRequest.AddHeader("sid", sessionID.ToString());
seatAvailRequest.AddHeader("Cookie", "sid = " + sessionID.ToString());
var seatResponse = client.Execute(seatAvailRequest);
List<SeatsInfo> seatsInfo = JsonConvert.DeserializeObject<List<SeatsInfo>>(seatResponse.Content);
//Further Implementation
}
}
}
Expected packet view in WireShark:
Actual packet view in WireShark:
sids are generated each time I successfully authorized to connect to the device, so they obviously differ.
The screenshot of my expected view is taken from the exact same block of codes in a plain console app. Everything works fine there. All the NuGet packages installed on the two programmes are of the same version, I even tried running them side-by-side but it did not help.
Debugging screenshots showed that the Parameters did, in fact, contain the Cookie header:
The header just did not go through for some reason. Would love to hear some thoughts in regards to this issue, many thanks in advance!
Current solution, just add another AddCookie() method:
seatAvailRequest.AddHeader("sid", sessionID.ToString());
seatAvailRequest.AddHeader("Cookie","sid = " + sessionID.ToString());
seatAvailRequest.AddCookie("sid", sessionID.ToString());
This somehow does not work with just AddCookie() or AddHeader(), so for now they are both going in. Hope this helps someone, and thank yall for reading!
So, in recap I was lacking the Cookie Header in the API call thus making the call being unsuccessful (the device did not authorized it).
After messing around with the AddCookie() method, particularly:
seatAvailRequest.AddCookie("sid", sessionID.ToString());
The device gave me a different error - 404 Not Found, before was 401 Unauthorized:
Finally my dumb mind resorted using both the AddCookie() along with AddHeader() and voilà:
It works! Only after 5 hours of painful debugging and mind-numbing searches. Although the new packet is a bit larger, but I do not have to worry about that for now, or in the future realistically speaking.
Thanks you all for just following along, still open for suggestions and improvements!