can not login automatically
Hi
Please help, I write code using HttpWebRequest and HttpWebResponse to login to yahoo mail but it was not successful. It displays the login form page only. Please let me know what is wrong with it. Here is my code:
using System;
using System.Net;
using System.IO;
using System.Text;
namespace WebDavNET
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string strLoginName = //my username here
string strPassword = //my passwd here
string retStr = "", tempStr = "";
HttpWebResponse webRespResult = null;
try
{
string strURL = "http://login.yahoo.com/config/login?";
string strPostData = String.Format("login={0}&passwd={1}",
strLoginName, strPassword);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(strURL);
webReq.Method = "POST";
webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
webReq.ContentType = "application/x-www-form-urlencoded";
// webReq.Proxy = WebProxy.GetDefaultProxy(); // does not work any more
webReq.ContentLength = strPostData.Length;
webReq.KeepAlive = true;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(strPostData);
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = strPostData.Length;
Stream inputStream = webReq.GetRequestStream();
inputStream.Write(bytes, 0, bytes.Length);
inputStream.Close();
webRespResult = (HttpWebResponse)webReq.GetResponse();
Stream ReceiveStream = webRespResult.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader streamRead = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = streamRead.Read(read, 0, 256);
while (count > 0)
{
tempStr = new String(read, 0, count);
retStr += tempStr;
count = streamRead.Read(read, 0, 256);
}
retStr.Trim();
Console.WriteLine(retStr);
Console.ReadLine();
}
catch (WebException e)
{
Console.WriteLine("\nMain 1 Exception raised!");
Console.WriteLine("\nMessage 1:{0}", e.Message);
Console.WriteLine("\nStatus 1:{0}", e.Status);
Console.WriteLine("Press any key to conntinue..........");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("\nMain 2 Exception raised!");
Console.WriteLine("Source 2:{0} ", e.Source);
Console.WriteLine("Message 2:{0} ", e.Message);
Console.WriteLine("Press key to continue..........");
Console.ReadLine();
}
finally
{
if (webRespResult != null)
{
webRespResult.Close();
}
}
}
}
}
|