Search code examples
c#system.netsystem.net.httpwebrequest

Code Works Fine But I am still not able to login into IRCTC Website


Everything Working Properly but we click on submitData() button i will show me login page instead of myhomepage,

so very first I navigate login page it will set the cookie, then cookie stored in cookie container code is

CookieContainer cookieJar = new CookieContainer();
public Form1()
{
    String captcha;
    InitializeComponent();
}

private void BUTTON_LOGIN_Click(object sender, EventArgs e)
{
    //Get captcha           
    var request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/loginHome.jsf");
    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
    request.KeepAlive = true;
    request.CookieContainer = cookieJar;
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";

    var response = request.GetResponse();
    using (var stream = response.GetResponseStream())
    {

    }

    // need array, different storage for every cookies
    //
    foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
    {
        request.CookieContainer.Add(c);
        MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
    }
}

Then I request for captcha using cookie

private void pictureBox1_Click(object sender, EventArgs e)  // This Event Refresh thr captcha
{
    //Get captch       

    var request =(HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/captchaImage");
    request.Accept = "image/webp,image/*,*/*;q=0.8";
    WebHeaderCollection myWebHeaderCollection = request.Headers;
    myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
    myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");
    request.KeepAlive = true;

    Uri target = new Uri("https://www.irctc.co.in");
    cookieJar.Add(new Cookie("language", "en_IN") { Domain = target.Host });

    request.CookieContainer = cookieJar;

    request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";


    var response = request.GetResponse();
    using (var stream = response.GetResponseStream())
    {

        pictureBox1.Image = Bitmap.FromStream(stream);
    }

    // need array, different storage for every cookies
    //
    foreach (Cookie c in cookieJar.GetCookies(response.ResponseUri))
    {
        request.CookieContainer.Add(c);
        MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
    }
}


private void button1_Click(object sender, EventArgs e)
{
    submitData();
}

private void displayHtml(String html)
{
    webBrowser1.Navigate("about:blank");
    while (webBrowser1.Document == null || webBrowser1.Document.Body == null)
        Application.DoEvents();
    webBrowser1.Document.OpenNew(true).Write(html);
}

And finally I submit data after filling captcha to text box and here is error this is redirect me login page again so whats going wrong.

private void submitData()
{
    try
    {
        String user = "myusername";
        String pass = "mypassword";
        String submit = "Submit";
        String captcha=richTextBox1.Text.ToString() ;

        ASCIIEncoding encoding = new ASCIIEncoding();
       // MessageBox.Show(captcha);
        string postData = "j_username=" + user + "&j_password=" + pass + "&j_captcha=" + captcha + "&submit=" + submit + "&tneg=" ;
        byte[] data = Encoding.UTF8.GetBytes(postData);

        Uri target = new Uri("https://www.irctc.co.in");



        cookieJar.Add(new Cookie("language", "en_IN"){ Domain = target.Host });
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://www.irctc.co.in/eticketing/home");
        request.CookieContainer = cookieJar;

        WebHeaderCollection myWebHeaderCollection = request.Headers;
        myWebHeaderCollection.Add("Accept-Language", "en-US;q=0.8");
        myWebHeaderCollection.Add("Upgrade-Insecure-Requests", "1");

        request.Method = "POST";
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
        request.ContentType = "application/x-www-form-urlencode";
        request.Referer = "https://www.irctc.co.in/eticketing/loginHome.jsf";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        request.ServicePoint.Expect100Continue = false;

   //     var bytes = Encoding.UTF8.GetBytes("action=login&from=http%3A%2F%2Fwww.dofus.com%2Ffr&login=user123&password=password1232F&remember=1");
     //   request.ContentLength = bytes.Length;

        request.ContentLength = data.Length;
        request.KeepAlive = true;
//      request.AutomaticDecompression = DecompressionMethods.GZip;
  //    request.AutomaticDecompression = DecompressionMethods.Deflate;

 //     request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
      // request.SendChunked = true;
     //   request.TransferEncoding = "gzip, deflate, br";
        request.Host = "www.irctc.co.in";

       //request.Headers.Add
        Stream stream = request.GetRequestStream();
        stream.Write(data, 0, data.Length);
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        stream = response.GetResponseStream();

        foreach (Cookie c in cookieJar.GetCookies(request.RequestUri))
        {

            MessageBox.Show("Cookie['" + c.Name + "']: " + c.Value);
        }
        StreamReader sr = new StreamReader(stream);
       //richTextBox1.Text = sr.ReadToEnd();
        String myhtml = sr.ReadToEnd().ToString();

        displayHtml(myhtml);

        sr.Close();
        stream.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message);
    }
}

Solution

  • In the method submitData()

    request.ContentType = "application/x-www-form-urlencode"
    

    here is a typo, you have missed one 'd'

    it should be like this :-

    request.ContentType = "application/x-www-form-urlencoded"
    

    ContentType is "application/x-www-form-urlencoded".

    If the server is unable to understand the form data type(in this case "application/x-www-form-urlencode" is unknown to the server),then it will reject the request and redirects the user to login page as you mentioned.

    You can also check Form ContentType in HTML Forms(w3.org) for more information.

    Hope this helps :)