6 Replies - 1914 Views - Last Post: 06 June 2010 - 01:04 PM Rate Topic: -----

#1 Guest_CorePP*


Reputation:

WebRequest

Posted 06 June 2010 - 12:29 AM

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();
        }


Is This A Good Question/Topic? 0

Replies To: WebRequest

#2 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: WebRequest

Posted 06 June 2010 - 01:12 AM

First of all, i'm not sure if it's legal to do things like this.
Anyway, there is a already tutorial about it here on DIC. Check it here.

Also, in your code, you're using wrongly StreamReader class:
You're trying to read from readStream after closing it, that will not work.
MSDN page
So you need to call ReadToEnd method before closing the readStream object.

string response = readStream.ReadToEnd();
readStream.Close();
return response;

Was This Post Helpful? 0
  • +
  • -

#3 Guest_corePP*


Reputation:

Re: WebRequest

Posted 06 June 2010 - 03:42 AM

Hi JITHU and thank you for your help,
The tutorial is using WebBrowser that doesnt work well without form application and cant be use in not single thread.

I am trying to login and save the cookies with WebRequest\Respones.
Was This Post Helpful? 0

#4 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: WebRequest

Posted 06 June 2010 - 11:22 AM

Try this code here, i haven't tested it, as i'm not a member of that site:

CookieContainer cookies = new CookieContainer();

// link = http://www.esl.eu/login/save
public string TryLogin(string link)
{
	/*
		- email_id is the name of the textbox where you insert your email address
		- password is the name of the textbox to insert password
	*/
        string postData = "email_id=EMAIL_ID&password=PASSWORD";
	byte[] data = new ASCIIEncoding().GetBytes(postData);
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);	
	request.CookieContainer = cookies;
	request.Method = "POST";
	request.ContentType = "application/x-www-form-urlencoded";
	request.ContentLength = data.Length;
	request.KeepAlive = true;
	request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3)";
	
	Stream requestStream = request.GetRequestStream();
	requestStream.Write(data, 0, data.Length);
	requestStream.Close();
	
	HttpWebResponse response = (HttpWebResponse)request.GetResponse();
	Stream receiveStream = response.GetResponseStream();
	StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

	string response = readStream.ReadToEnd();
	
	response.Close();
	readStream.Close();

	return response;
}



Now you need to parse the response, to understand if you're logged in.
For ex: you can search for a message that the site outputs when you login.

After you've successfully done Login, whenever you make a request to the site, you need to pass
cookies too, that is created in the Login method.
request.CookieContainer = cookies;


There are no standard ways to login, as each site is different. So it's important that you dedicate some time looking into the various pages of the site, in order to understand it's structure.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_corePP*


Reputation:

Re: WebRequest

Posted 06 June 2010 - 12:23 PM

Works just perfect!! Awesome :D
Very thanks :)

But, after im trying to reach the next page with the cookies that I created, it still doesnt recognize me :|

        public static CookieContainer cookies = new CookieContainer();

        static void Main(string[] args)
        {
            string link = "http://www.esl.eu/login/save";
            TryLogin(link);
            AfterLogin("http://www.esl.eu/il/bfbc2/gather/");
        }

        public static void TryLogin(string link)
        {
            /*
                    - email_id is the name of the textbox where you insert your email address
                    - password is the name of the textbox to insert password
            */
            string postData = "email_id=EMAIL&password=PASSWORD";
            byte[] data = new ASCIIEncoding().GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
            request.CookieContainer = cookies;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3)";

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

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

        public static void AfterLogin(string link)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            TextWriter tW = new StreamWriter("C:/web.txt");
            tW.Write(readStream.ReadToEnd());
            tW.Close();
        }

Was This Post Helpful? 0

#6 Guest_corePP*


Reputation:

Re: WebRequest

Posted 06 June 2010 - 12:25 PM

NVM, I forgot to contain the cookies :D
Was This Post Helpful? 0

#7 JITHU  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 62
  • View blog
  • Posts: 201
  • Joined: 02-July 07

Re: WebRequest

Posted 06 June 2010 - 01:04 PM

good to know that it's working!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1