Tuesday, August 24, 2010

Login to website using VB.NET

VB.NET

Dim cookieJar As New Net.CookieContainer()
Dim request As Net.HttpWebRequest
Dim response As Net.HttpWebResponse
Dim strURL As String

Try
'Get Cookies
strURL = "https://login.facebook.com/login.php"
request = Net.HttpWebRequest.Create(strURL)
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
request.Method = "GET"
request.CookieContainer = cookieJar
response = request.GetResponse()

For Each tempCookie As Net.Cookie In response.Cookies
cookieJar.Add(tempCookie)
Next

'Send the post data now
request = Net.HttpWebRequest.Create(strURL)
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
request.Method = "POST"
request.AllowAutoRedirect = True
request.CookieContainer = cookieJar

Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
writer.Write("email=username&pass=password")
writer.Close()
response = request.GetResponse()

'Get the data from the page
Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
Dim data As String = stream.ReadToEnd()
response.Close()

If data.Contains("titleFacebook") = True Then
'LOGGED IN SUCCESSFULLY
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try


C#.NET


CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request;
HttpWebResponse response;
string strURL;

//Get Cookies
strURL = "https://login.facebook.com/login.php";
request = (HttpWebRequest)WebRequest.Create(strURL);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
request.Method = "GET";
request.CookieContainer = cookieJar;
response = (HttpWebResponse)request.GetResponse();

foreach (Cookie tempCookie in response.Cookies)
{
cookieJar.Add(tempCookie);
}


//Send the post data now
request = (HttpWebRequest)WebRequest.Create(strURL);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3";
request.Method = "POST";
request.AllowAutoRedirect = true;
request.CookieContainer = cookieJar;

StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write("email=prasanthsethu@gmail.com&pass=1");
writer.Close();
response = (HttpWebResponse)request.GetResponse();

//Get the data from the page
StreamReader stream = new StreamReader(response.GetResponseStream());
string data = stream.ReadToEnd();
response.Close();

if (data.Contains("title Facebook"))
{
}

No comments:

Post a Comment