Register yourself on http://www.railpnrapi.com/ and get/generate API Key & API Secret.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace RailPnrAPI_SampleCode
{
class Program
{
static void Main(string[] args)
{
//set your credentials
var apiCreds = new Dictionary
{
{“Key”, “”},
{“Secret”, “”}
};
var pnr = “1234567890”; //test pnr
var requestUrlTemplate = “http://railpnrapi.com/api/check_pnr/pnr/”+pnr+”/format/json/pbapikey/” +
apiCreds[“Key”] + “/pbapisign/{0}”;
//Get All the request parameter
var paramsSet = new Dictionary
{
{“pnr”, pnr},
{“format”, “json”},
{“pbapikey”, apiCreds[“Key”]}
};
//sort the keys and concat their values
var inputString = paramsSet.OrderBy(x => x.Key).Select(x => x.Value).Aggregate((a, b) => a + b).ToLower();
//var keyBytes = Encoding.ASCII.GetBytes(apiCreds[“Secret”]);
var keyBytes = Encoding.ASCII.GetBytes(apiCreds[“Secret”]);
// compute hash
var signature = ComputeHMACSHA1(inputString, keyBytes);
var request = WebRequest.CreateHttp(string.Format(requestUrlTemplate, signature));
var response = request.GetResponse();
// parse JSON response using Newtonsoft.Json
dynamic json = JsonConvert.DeserializeObject(new StreamReader(response.GetResponseStream()).ReadToEnd());
// process/display response
Console.WriteLine(“Response Code: {0}”,json.response_code);
Console.WriteLine(“PNR: {0}”, json.pnr);
Console.WriteLine(“Train No.: {0}”, json.train_num);
Console.WriteLine(“Train Name: {0}”, json.train_name);
Console.WriteLine(“DOJ: {0}”, json.doj);
Console.WriteLine(“From Station (Code/Name): {0}/{1}”, json.from_station.code, json.from_station.name);
Console.WriteLine(“To Station (Code/Name): {0}/{1}”, json.to_station.code, json.to_station.name);
Console.WriteLine(“Reservation Upto (Code/Name): {0}/{1}”, json.reservation_upto.code, json.reservation_upto.name);
Console.WriteLine(“Boarding Point (Code/Name): {0}/{1}”, json.boarding_point.code, json.boarding_point.name);
Console.WriteLine(“Class: {0}”, json.@class);
Console.WriteLine(“No. of Passengers: {0}”, json.no_of_passengers);
Console.WriteLine(“Chart Status: {0}”, json.chart_prepared);
Console.WriteLine();
Console.WriteLine(“Passengers: n********************************”);
foreach (var passenger in json.passengers)
{
Console.WriteLine(“Passenger #:{0}, Booking Status:{1}, Current Status:{2}”, passenger.sr, passenger.passengers, passenger.current_status);
}
Console.WriteLine();
Console.WriteLine(“********************************”);
Console.ReadLine();
}
private static string ComputeHMACSHA1(string input, byte[] key)
{
var myHmacSha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.ASCII.GetBytes(input);
var stream = new MemoryStream(byteArray);
byte[] hashValue = myHmacSha1.ComputeHash(stream);
return string.Join(“”, Array.ConvertAll(hashValue, b => b.ToString(“x2”)));
}
}
}
Here’s the sample console output:
Sample Code Output |
Sample Code in VB
————————
End Module
——————–
Download Sample Code for C# & VB Here: Sample Codes Rail PNR API
thanks your code works perfectly .
i m unable to imolement your code in vb.net after convert it.
plz send me vb.net code if possible….
thank you.
thanks razz! I'm glad it helped 🙂
Could you please get me the code in java, as am not aware of vb
Dear Singh,
I've updated the post with sample code in VB also, please have a look. Thanks!
Thanks for reaching out!
Well, I'm not a java guy but will surely try to get you the code soon!
How can I generate the HMAC in Node JS? Please help
I’m happy I located this blog thanks for share….
HELLO Sunny Sir,
I want Create Pnr check site , So Please Help How to use Json Api In WordPress Based site. I Am But Api.