Posting data using httpwebrequest.

S

shankararaman.s

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.
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
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.c
om/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.

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,
 
T

tdavisjr

David said:
You might want to change your account password now that you have just told
everyone what it is.

WOW. This was not a wise thing to do.


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


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.
 
S

Shankar

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
 
J

Joerg Jooss

Thus wrote Shankar,
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.c
om/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);

CookieContainer tracks cookies automatically. You don't need to copy them
yourself (and at this point, there will be no session cookie, yet).
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);

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,
 
S

Shankar

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.
 
S

Shankar

Sorry. I am getting an emptystring as response for response.Server

Thanks & Regards,
Shankara.
 
J

Joerg Jooss

Thus wrote Shankar,
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.

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,
 

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