Pages

Monday, 15 July 2013

PNR Status by Web Scripting Method using c#.net

Introduction:

 Here I will explain how to check PNR Status through programmatically using c#.net

 PNR STATUS


                     Indian railway is one of the largest railway network and employer in the world, most of the people visit indian railway website to check their PNR status of the ticket,Today we will create a widgit,Ths widgit provide you a easy and clean interface to check your Passenger Name Record ( PNR ) status of your IRCTC reservation . To check your PNR status simply copy this code and paste your visual studio enter your 10 digit PNR number and hit the Get PNR Status button.

First, we need to design the page like below.
after that write the c# method for request/response pnr status.

public string GetPNRStatus(string sPNR)
{
//indianrail url
string URI = http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi;
//passing the pnr status to indian rail with help of querystring
string Parameters = Uri.EscapeUriString("lccp_pnrno1=" + sPNR + "&submitpnr=Get Status");
//creating the http request with help of httpwebrequest
System.Net.
HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(URI);
//HTTP POST Headers
req.ContentType = "application/x-www-form-urlencoded";
//HTTP Host Name
req.Host = "www.indianrail.gov.in";
/You can use your own user-agent.
req.UserAgent =
"Mozilla/5.0 (compatible; MSIE 7.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0) DELL;Venue Pro";

req.Headers.Add(
HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");

req.Headers.Add(
HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

req.KeepAlive =
true;
req.Referer =
"http://www.indianrail.gov.in/pnr_stat.html";
req.Accept =
"text/plain";
req.Method=
"POST";
//Byte size calculation before sending request.
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
//Requesting the pnr status from indianrail.gov.in
System.IO.
Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

//get response from indianrail.gov.in
System.Net.
WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.
StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd();
}
after writing this method we can call button click event like below.
protected void BtnPNRStatus_Click(object sender, EventArgs e)
{
try
{
Response.Write(GetPNRStatus(txtPNRNumber.Text));
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

Finally we got result, see the results below.



 Note: This method involves scraping the Indian Railways website, as they do not have an API in place yet. This is purely for educative purposes, and is most likely illegal to implement in a full-feature application.


 

4 comments:

  1. hi,

    good work..........

    It is possible to develop javascript?

    ReplyDelete
  2. pnr status by web sripting method is nice thanks
    Indian railway pnr status

    ReplyDelete
  3. //indianrail url
    string URI = http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi ;

    error line -- : invalid..what to do?

    ReplyDelete
  4. //indianrail url
    string URI = http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi ;

    invalid : in this code..what to do?

    ReplyDelete