posting data to a web form using HttpWebRequest

S

shaily903

I already posted this problem on group but didn't hear solution from
anybody. So I planned to post it again.

I am very new to writing a web-cralwer which programatically loging to
a site.
My Requirements are: I need to browse the
"http://mail.rediff.com/cgi-bin/login.cgi" site daily to see if
there is new Email present in my Inbox. I want to automate this whole
process by writing a web crawler which will crawl these site,enter my
login username and password then let me know if there is any new email.

I started writing like below:

Currently the way I am doing is :
private void login()
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create("http://mail.rediff.com/cgi-bin/login.cgi");

req.Method = "Post";
string s = "login=shaily&passwd=mypasswd";
req.CookieContainer = new CookieContainer();
req.ContentType =
"application/x-www-form-urlencoded";
//Set the POST data in a buffer
byte[] PostBuffer =
Encoding.GetEncoding(1252).GetBytes(s);
//Specify the length of the buffer
req.ContentLength = PostBuffer.Length;
//Open up a request stream
Stream RequestStream = req.GetRequestStream();

//Write the POST data
RequestStream.Write(PostBuffer, 0,
PostBuffer.Length);
//Close the stream
RequestStream.Close();

//Create the Response object
HttpWebResponse Response =
(HttpWebResponse)req.GetResponse();
//Create the reader for the response
StreamReader sr = new
StreamReader(Response.GetResponseStream(),Encoding.GetEncoding(1252));
//Read the response
string t = sr.ReadToEnd();
//Close the reader, and response
sr.Close();
Response.Close();
MessageBox.Show(t);
CreateFile(t,"c:\\Changelog");
}
private void CreateFile(string buffer,string filename)
{
StreamWriter outStream = new StreamWriter(
filename );
outStream.Write(buffer);
outStream.Close();
}


Problem: The respond which I am getting does not show me the inbox of
mine where I can check the number of new mails. It seems to me that it
is not posting the data to a form properly. Am I doing something wrong?

What should I do to "login to my inbox" and retrieve the different
values from there(E.g no of new mails, Total number of mails in inbox )
 
F

Feroze [msft]

The best way to get this working, is to do a network sniff of the login
using a browser. Then, do a sniff using your program. Compare and see the
difference. Fix your program to confirm to what the browser is doing. Rinse,
Lather, Repeat...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top