• (2 Pages)
  • +
  • 1
  • 2

C# - Log in to Website Programmatically How to log in to a website with code.

#16 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 24 January 2011 - 08:41 AM

View Postjabcek, on 24 January 2011 - 06:22 AM, said:

what about my problem, i am trying to send a message inside the site. Here is the code:
     <form action='http://www.powerplaymanager.com/action/action_send_mail.php' method='post'>
     <tr>
      <td class='ppm_body' valign='top' width='100%'>
    

        <div class='universal_form_element'>

         <strong>Prejemnik</strong>
         <input type='text' name='whom' size='30' value='' />
        </div>
    
        <div class='universal_form_element'>
         <strong>Zadeva</strong>
         <input type='text' name='subject' size='30' maxlength='50' value='' />
        </div>
    
        <div class='universal_form_element'>

         <strong>Besedilo</strong>
         <textarea name='text' rows='20' cols='100'></textarea>
        </div>
    
        <div>
         <input type='hidden' name='admin' value='0' />
         <input type='hidden' name='id_sort' value='1' />
         <input type='hidden' name='url_send_ok' value='http://ppm.powerplaymanager.com/sl/posta.html?data=unread' />
         <input type='hidden' name='url_send' value='http://ppm.powerplaymanager.com/sl/posta.html?data=send' />

         <input type='submit' value="Pošlji" />
        </div>

      </td>
     </tr>
     </form>



i tryed like this:

      string reciever= "testname";
            string subject= "Welcome";
            string text= "test123";
            string admin="0";
            string id_sort="1";
            string url_send_ok = "http%3A%2F%2Fppm.powerplaymanager.com%2Fsl%2Fposta.html%3Fdata%3Dunread";
            string url_send = "http%3A%2F%2Fppm.powerplaymanager.com%2Fsl%2Fposta.html%3Fdata%3Dsend";
            string submit="Pošlji";

            string postData1 = string.Format("whom={0}&subject={1}&text={2}&admin={3}&id_sort={4}&url_send_ok={5}&url_send={6}&submit={7}", reciever, subject, text, admin, id_sort,url_send_ok,url_send,submit);
            string url1="http://ppm.powerplaymanager.com/sl/posta.html?data=send";
            b.Navigate(url1, "", enc.GetBytes(postData1), "Content-Type: application/x-www-form-urlencoded\r\n");

            

But the only thing that is insered is reciever. All other fields are empty.


Never mind this, i had wrong url...
this is the correct one.
url1="http://www.powerplaymanager.com/action/action_send_mail.php"
Was This Post Helpful? 0
  • +
  • -

#17 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 24 January 2011 - 09:13 AM

It is created with Windows Form application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace baltracker
{
   public partial class Form1 : Form
   {
      string url = "http://www.filmai.in";
      string username = "login";
      string password = "pass";
      public Form1()
      {
         InitializeComponent();
         Login();
      }

private void Login()
{
	 WebBrowser b = new WebBrowser();
	 b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	 b.Navigate(url);
}

private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
	 WebBrowser b = sender as WebBrowser;
	 //string response = b.DocumentText; //same error here

	 // looks in the page source to find the authenticity token.
	 // could also use regular exp<b></b>ressions here.
	 //int index = response.IndexOf("authenticity_token");
	 //int startIndex = index + 41;
	 //string authenticityToken = response.Substring(startIndex, 40);

	 // unregisters the first event handler
	 // adds a second event handler
	 b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	 b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

    string postData = string.Format("login_name={0}&login_password={1}&image.x=0&image.y=0&login=submit", username, password);
	 ASCIIEncoding enc = new ASCIIEncoding();

	 //  we are encoding the postData to a byte array
    b.Navigate(url, "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
}

private void b_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
	 WebBrowser b = sender as WebBrowser;
	 string response = b.DocumentText;
    //MessageBox.Show(response);
    //richTextBox1.Text = response;
    //if (response.Contains(username))
    //{
    //   label1.Text = "Prisijungta";
    //  // MessageBox.Show("Login Successful");
    //}
    //else label1.Text = "Atsijungta";
    MessageBox.Show("baigta");
}

private void Form1_Load(object sender, EventArgs e)
{

}
   }
}


Was This Post Helpful? 0
  • +
  • -

#18 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 24 January 2011 - 02:22 PM

Is it possible to navigate to other pages of website? when signed in? I tried:
         WebBrowser b = sender as WebBrowser;

         WebClient web = new WebClient();
         b.Navigate("different page, same website");
         string response = b.DocumentText;
         richTextBox1.Text = response;

But it returns same webpage.
Was This Post Helpful? 0
  • +
  • -

#19 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 24 January 2011 - 02:28 PM

yes its possible, and you stay loged in. but you would need another event handler, like this
b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

     b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted3);

private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//code here
}



or sometimes you can halp with this
while (b.ReadyState != WebBrowserReadyState.Complete)
                    {
                        Application.DoEvents();
                    }

so that page is loaded and you get the correct data.

This post has been edited by jabcek: 24 January 2011 - 02:31 PM

Was This Post Helpful? 1
  • +
  • -

#20 full_int  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 11

Posted 24 January 2011 - 02:30 PM

But where to place Navigate() function?
Was This Post Helpful? 0
  • +
  • -

#21 jabcek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 13-January 11

Posted 24 January 2011 - 02:33 PM

if you use event handler it doesnt matter, coz it completes the void than it goes to event handler. If you use the while loop you have to use it before while loop.
Was This Post Helpful? 0
  • +
  • -

#22 Guest_Adel Boutros*


Reputation:

Posted 11 February 2011 - 04:43 AM

Heyyy,

I really like your post and it's just what I'm searching for, but it didn't work for my website.
What I want to do is login to this website : http://wallet.wise.n...mtjsf/index.jsp . I tried to change your code using Tamper but it doesn't work, I get a response but the response is an Exception handler which contains "incomplete literal/length tree".
Here's the code I used:
        string url = "http://wallet.wise.net.lb/accountmgmtjsf/index.jsp";
        string username = "l337213@wise.net.lb";
        string password = "CEM5S7ZHW2";
        string form1_x = "32";
        string form1_y = "26";
        string lnumber = "";
        string form1_hidden = "form1_hidden";
        string ViewState;

        private void Login()
        {
            WebBrowser b = new WebBrowser();
            b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
            b.Navigate(url);
        }

        private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser b = sender as WebBrowser;
            string response = b.DocumentText;

            // looks in the page source to find the authenticity token.
            // could also use regular expressions here.
            int index = response.IndexOf("id=\"javax.faces.ViewState\"");
            int startIndex = index + 34;
            ViewState = response.Substring(startIndex, 2273);
            label1.Text = ViewState.Length.ToString();
            // unregisters the first event handler
            // adds a second event handler
            b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
            b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

            // format our data that we are going to post to the server
            // this will include our post parameters.  They do not need to be in a specific
            //    order, as long as they are concatenated together using an ampersand ( & )
            string postData = string.Format("form1:username={0}&form1:password={1}&form1:j_id_jsp_1792837252_9.x={2}&form1:j_id_jsp_1792837252_9.y={3}&lnumber={4}&form1_hidden={5}&javax.faces.ViewState={6}", username, password, form1_x, form1_y, lnumber, form1_hidden, ViewState);

            ASCIIEncoding enc = new ASCIIEncoding();

            //  we are encoding the postData to a byte array
            b.Navigate("http://wallet.wise.net.lb/accountmgmtjsf/login.jsf", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
        }

        private void b_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser b = sender as WebBrowser;
            string response = b.DocumentText;

            if (response.Contains("LOG OUT"))
            {
                MessageBox.Show("Login Successful");
            }
        }



I have attached as well the Response.
Please help me. I really need this to work for my final project in University

Regards,

Adel Boutros

Attached File(s)

  • Attached File  bla1.htm (4.41K)
    Number of downloads: 123

Was This Post Helpful? 0

#23 cxt_hut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-August 11

Posted 07 August 2011 - 05:23 PM

View Posteclipsed4utoo, on 27 January 2010 - 10:22 AM, said:

In this tutorial, will show how to log into a website through code. I am going to use Twitter in my example.
THIS IS FOR EDUCATIONAL PURPOSES ONLY. I WOULD NOT ADVISE THE USE OF THIS TO ALWAYS LOG-IN TO TWITTER.

First, you will need to download and install Tamper Data. It’s an add-on for Firefox that allows you to view and tamper with GET/POST web request data. We won’t be doing any tampering. We will be using it to view what POST parameters the page is expecting.

Next, navigate to the log in page for Twitter(http://twitter.com/login). Once the page has loaded, go to Tools –> Tamper Data to open Tamper Data. At the top of Tamper Data, click the Start Tamper button. After clicking the button, click the “Sign In” button on Twitter’s log in page. Once you hit the Sign In button, Tamper Data will prompt you with this popup….

Attachment attachment

Click “Tamper”. You will then be presented with this window…

Attachment attachment

If you notice on the right-hand side of the window, you will see the POST parameters. These are:

authenticity_token
session[username_or_email]
session[password]
commit

Now that we have those, we can close the Tamper Data window, and close Firefox.

Now for the code. The code is actually fairly simple. We are just going to use the WebBrowser class to make the requests to the server to get the html source of the page. This will give us the “authenticity_token” for us to use in the POST request.

string url = "https://twitter.com/login";
string username = "someUserName";
string password = "somePassword";
string commit = "Sign+In"; //this matches the data from Tamper Data

private void Login()
{
	 WebBrowser b = new WebBrowser();
	 b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	 b.Navigate(url);
}

private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
	 WebBrowser b = sender as WebBrowser;
	 string response = b.DocumentText;

	 // looks in the page source to find the authenticity token.
	 // could also use regular expressions here.
	 int index = response.IndexOf("authenticity_token");
	 int startIndex = index + 41;
	 string authenticityToken = response.Substring(startIndex, 40);

	 // unregisters the first event handler
	 // adds a second event handler
	 b.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted);
	 b.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(b_DocumentCompleted2);

	 // format our data that we are going to post to the server
	 // this will include our post parameters.  They do not need to be in a specific
	 //	order, as long as they are concatenated together using an ampersand ( & )
	 string postData = string.Format("authenticity_token={2}&session[username_or_email]={0}&session[password]={1}&commit={3}", username, password, authenticityToken, commit);

	 ASCIIEncoding enc = new ASCIIEncoding();

	 //  we are encoding the postData to a byte array
	 b.Navigate("https://twitter.com/sessions", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
}

private void b_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
	 WebBrowser b = sender as WebBrowser;
	 string response = b.DocumentText;

	 if (response.Contains("Sign out"))
	 {
		 MessageBox.Show("Login Successful");
	 }
}



And that’s all you need to do. You can now use the response variable to see the tweets that are in your timeline.

why i don't login to a website https
example: My link
please help me!

This post has been edited by cxt_hut: 07 August 2011 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#24 buzgibi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-August 11

Posted 29 August 2011 - 01:44 PM

Thanks for tutorial..
I can login site now..
But,
how can i navigate post data results in web browser..
I will send post data to website for search then i cant see navigate for results..
Was This Post Helpful? 0
  • +
  • -

#25 mohammedokasha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 12

Posted 11 February 2012 - 03:33 AM

what if i longing successful to any site : and that site have restricted page with [session or cookie ] for example ==> (info.php)

how i can extract that from that page (info.php) using htmlagilitypack after login successful ??
Was This Post Helpful? 0
  • +
  • -

#26 sen_saven  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-April 09

Posted 23 March 2012 - 07:41 AM

hello,

great tutorial.

Everything is fine for most of the sites I tried it, but on one occasion I get the 302 status code when I post the data
and the flags LOAD_DOCUMENT_URI and LOAD_INITIAL_DOCUMENT_URI.

After that,an automatic GET is being made which returns the 200 status code(everything ok) and the flags LOAD_DOCUMENT_URI, LOAD_INITIAL_DOCUMENT_URI and LOAD_REPLACE.

The problem is that this GET is not captured by the tamper data since the next GET that is being recognised and I cancel is not the one that actually downloads the page.

Any hint?

thank you
Was This Post Helpful? 0
  • +
  • -

#27 Musketeer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-December 11

Posted 22 July 2012 - 03:22 AM

Hi guys.

Indeed the best tutorial I could find about this topic.

I still have a problem though, the website I'm trying ( http://s2.kingsera.org/ ) doesn't return anything like the "authentication token". So what am I supposed to do in this case?

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#28 adam.bleech  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-April 13

Posted 28 April 2013 - 08:33 AM

I have a question about using this webbrowser class. When fetching the data for a specified url how does this class handle cookies? Aren't the cookies required to be able to be logged into sites such as twitter? The reason I ask is because I want to create a program that retrieves information from questionable web sites safely, in no way handling cookies, java, flash, or anything else that can create a security hole or cause harm to the user. I was going to do this the old fashioned way using sockets but thought this could be me a much faster method. I assume this webbrowser class is related to Internet Explorer and was wondering what incite you had as to what kind of threats using this class would open you up to.
Was This Post Helpful? 0
  • +
  • -

#29 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Posted 29 April 2013 - 12:10 PM

I don't think this handles cookies at all; you do this yourself using the CookieContainer class and its associates.
Was This Post Helpful? 0
  • +
  • -

#30 kareenva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: Yesterday, 03:23 AM

Posted Yesterday, 03:30 AM

I have a username and password on: https://my.stc.com.s...online/en/login

When I browse the URL above, I get
https://my.stc.com.s...P0c6McFQEI4Sqc/

The SjzS0NDSxtDQ0NDHQj9CPykssy0xPLMnMz0vMAfGjzOL9PdyC3d3NQt2NLQwsDDx9nB2NTb09jQ08jfWDU_P0c6McFQEI4Sqc changed every time.

With Tamper Data I get username, password, and login_btn as parameters, but I cannot log in with them by the above code!

Could you please help me?!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2