0

Hi

I am trying to connect and get a WebResponse of a CONNECTED user.
For that, I should first connect to the webSite, but I can figure out how.
I already read like 20 pages about cookies and stuff and still cant understand this stuff.

The site im trying to connect is http://www.esl.eu/ - http://www.esl.eu/login

Here is my code

public static string getString(string link)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);

            // Set credentials to use for this request.
            CredentialCache cache = new CredentialCache();
            //?? cache.Add(new Cookie(

            request.CookieContainer = cache;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

            response.Close();
            readStream.Close();

            return readStream.ReadToEnd();
        }
3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by sknake
0

Thats what I get after conncetion

Logged in!
You have successfully logged in. A cookie has been stored on your computer. If you wish to logout, do so using the link in the top right hand corner of the page.


proceed

0

Try this....

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmESL : Form
  {
    static CookieContainer container;
    const string username = @"user@name.com";
    const string password = @"password";

    public frmESL()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string s = getString(@"http://www.esl.eu/#/myteams/");
      System.Diagnostics.Debugger.Break();
    }

    public static string getString(string link)
    {
      DoLogin();

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
      request.Method = "GET";
      request.AllowAutoRedirect = true;
      request.CookieContainer = container;
      request.Referer = @"http://www.esl.eu/";
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
        using (Stream receiveStream = response.GetResponseStream())
        {
          using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
          {
            return readStream.ReadToEnd();
          }
        }
      }
    }

    private static void DoLogin()
    {
      if (container != null)
        return;

      System.Net.ServicePointManager.Expect100Continue = false;

      container = new CookieContainer();

      //You should be able to POST directly to the login page without first doing a GET,
      //however this tips off crafty web admins that automated logins are taking place.
      //This may also solve issues if they set a cookie on a GET request
      //Basically -- This is probably optional, but do it anyway.
      #region download login page
      {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://www.esl.eu/login");
        request.Method = "GET";
        request.AllowAutoRedirect = true;
        request.Referer = @"http://www.esl.eu/";
        request.CookieContainer = container;
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
          using (Stream receiveStream = response.GetResponseStream())
          {
            const int read_length = 2048;
            byte[] recv_buff = new byte[read_length];
            int read;
            do
            {
              read = receiveStream.Read(recv_buff, 0, read_length);
            }
            while (read >= read_length);
          }
        }
      }
      #endregion

      #region login
      {
        byte[] buffer = GetFormData(username, password);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://www.esl.eu/login/save/");
        request.Method = "POST";
        request.AllowAutoRedirect = true;
        request.Referer = @"http://www.esl.eu/login";
        request.CookieContainer = container;
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = buffer.Length;
        using (Stream s = request.GetRequestStream())
        {
          s.Write(buffer, 0, buffer.Length);
          s.Close();
        }
        using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        {
          GC.KeepAlive(resp);
        }
      }
      #endregion
    }

    private static byte[] GetFormData(string Email, string Password)
    {
      const string login_format = @"email_id={0}&password={1}&duration=forever&x=73&y=13";

      string login_line = string.Format(login_format,
        Email,
        Password);

      return System.Text.UTF8Encoding.UTF8.GetBytes(login_line);
    }



  }
}

I have verified it works with "My Teams". You will receive a not_logged_in string if you are not logged in, else a "My Teams" panel.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.