Groups | Blog | Home
all groups > c# > february 2006 >

c# : Posting data using httpwebrequest.


shankararaman.s NO[at]SPAM gmail.com
2/16/2006 10:11:06 PM

Hi,

I am trying to develop an interface which will fetch all my Yahoo
mails. I am not able to sign in to yahoo by posting the form with my
username & password. Please find my code below and correct me where am
going wrong.

string result;
System.Net.HttpWebRequest request,request1;
System.Net.HttpWebResponse response,response1;
StreamReader sr;StreamWriter sw;
request1 = (System.Net.HttpWebRequest)
System.Net.WebRequest.Create("http://groups.yahoo.com/");
response1 = (System.Net.HttpWebResponse) request1.GetResponse();
string postData = "login=shansulak_2001&passwd=shansulak";
request =
(HttpWebRequest)System.Net.HttpWebRequest.Create("http://login.yahoo.com/config/login?.intl=us&.src=ygrp&.done=http://groups.yahoo.com");

request.Method = "POST";
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
Encoding utf8 = new UTF8Encoding();
byte[] content = utf8.GetBytes(postData);
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);

}
response = (HttpWebResponse)request.GetResponse();
sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
Response.Write(result);

Thanks in advance for your help.

Regards,
Shankara.
tdavisjr
2/17/2006 12:29:43 PM

[quoted text, click to view]

WOW. This was not a wise thing to do.



[quoted text, click to view]
Joerg Jooss
2/17/2006 7:31:50 PM
Thus wrote shankararaman.s@gmail.com,

[quoted text, click to view]

I would assume Yahoo sends a session cookie after successfully logging on.
You need to use a System.Net.CookieContainer instance to keep track of cookies
in your session.


Cheers,
--
Joerg Jooss
news-reply@joergjooss.de

David
2/17/2006 8:04:01 PM
You might want to change your account password now that you have just told
everyone what it is.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


[quoted text, click to view]

Shankar
2/19/2006 10:22:57 PM
I have included code for saving cookies. But still not working. Please
Help.

string result;
System.Net.HttpWebRequest request,request1;
System.Net.HttpWebResponse response,response1;
StreamReader sr;StreamWriter sw;
request1 = (System.Net.HttpWebRequest)
System.Net.WebRequest.Create("http://groups.yahoo.com/");
response1 = (System.Net.HttpWebResponse) request1.GetResponse();

string postData = "login=id_for_testing&passwd=testingGroup";
request =
(HttpWebRequest)System.Net.HttpWebRequest.Create("http://login.yahoo.com/config/login?.intl=us&.src=ygrp&.done=http://groups.yahoo.com");

request.Method = "POST";
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 100000;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(response1.Cookies);
Encoding utf8 = new UTF8Encoding();
byte[] content = utf8.GetBytes(postData);
request.ContentLength = content.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(content, 0, content.Length);
}
response = (HttpWebResponse)request.GetResponse();
sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
Response.Write("Server: "+response.Server +" Code:
"+response.StatusCode + " Desc: " + response.StatusDescription);
Response.Write(result);

Thanks & Regards,
Shankara.

Thanks a lot for pointing out that I have leaked out my mail passwd
Dave :-) I have changed it
Joerg Jooss
2/20/2006 8:17:24 PM
Thus wrote Shankar,

[quoted text, click to view]

CookieContainer tracks cookies automatically. You don't need to copy them
yourself (and at this point, there will be no session cookie, yet).

[quoted text, click to view]

So what happens at this point? Do you get redirected back to the login page?
Do you get an error page?

You should also verify that Yahoo uses cookies. I've blindly assumed that,
because it's the most common approach for session tracking, but if they're
using URL rewriting, all of the above won't really help.

Cheers,
--
Joerg Jooss
news-reply@joergjooss.d

Shankar
2/21/2006 12:16:57 AM
Hi Jeorg,

I get a page where it says " The page cannot be found. HTTP 404 - File
not found". I get the Statuscode as "OK" and StatusDescription also
"OK". I am not getting emptystring for response.Server. I think Yahoo
does use cookies. Because, when I donot use the cookie container, it
says, "cookies rejected" error.

Thanks & Regrds,
Shankara.
Shankar
2/21/2006 3:31:44 AM
Sorry. I am getting an emptystring as response for response.Server

Thanks & Regards,
Shankara.
Joerg Jooss
2/21/2006 7:58:33 PM
Thus wrote Shankar,

[quoted text, click to view]

That means you were either redirected to a non-existing page or you requested
a non-existing page.

It's not uncommon that there's no Server header. Many organizations don't
want to reveal what kind of server they are running, due to security concerns.

Cheers,
--
Joerg Jooss
news-reply@joergjooss.de

AddThis Social Bookmark Button