Gufran
Member
- Joined
- Nov 3, 2011
- Messages
- 6
- Programming Experience
- Beginner
hi guys,
I am just a newbie to VB .NET, It may be a silly question for you but really it is bugging me from 3 days. I googled alot for the solution but cant find a way around.
here is the problem, I am logging into a website and storing the cookie, right after then I want to navigate to another page that is redirecting me to the login page again. I know I am not using the cookie the way it should be, can somebody kick me in right direction ?
here is the codes
I am just a newbie to VB .NET, It may be a silly question for you but really it is bugging me from 3 days. I googled alot for the solution but cant find a way around.
here is the problem, I am logging into a website and storing the cookie, right after then I want to navigate to another page that is redirecting me to the login page again. I know I am not using the cookie the way it should be, can somebody kick me in right direction ?
here is the codes
VB.NET:
.
.
.
Private Sub loginbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loginbtn.Click
dim url as system.uri = new system.uri("http://www.somewebsite.com/login.php")
request = WebRequest.Create(url)
loginid.Enabled = False
loginpass.Enabled = False
loginbtn.Enabled = False
Me.Text = "Logging in..."
request.Method = "POST"
postData = "MobileNoLogin=" + loginid.Text + "&" + "LoginPassword=" + loginpass.Text
byteArray = Encoding.UTF8.GetBytes(postData)
request.CookieContainer = cookiejar
request.ContentLength = byteArray.Length
request.ContentType = "application/x-www-form-urlencoded"
request.KeepAlive = True
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
response = request.GetResponse()
stat = (CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd
messagebox.show(responsefromserver") [B] '----------->> Shows that I am logged in :)[/B]
reader.Close()
dataStream.Flush()
dataStream.Close()
response.Close()
End Sub
Private Sub sendbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbtn.Click
' HERE IT REDIRECT ME TO THE LOGIN PAGE AGAIN :(
request = WebRequest.Create("http://www.website.com/submitmessage.php")
request.Method = "POST"
postData = "recepient=somerecepient&Message=Hi, I am testing my application to send messages"
byteArray = Encoding.UTF8.GetBytes(postData)
request.CookieContainer = cookiejar
request.ContentLength = byteArray.Length
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11"
request.ContentType = "application/x-www-form-urlencoded"
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
response = request.GetResponse()
dataStream.Close()
stat = (CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd
messagebox.show(responsefromserver) [B]'--------------> Here I can see the redirection to the login page :([/B]
Catch ex As Exception
If ex.ToString.Length <> 0 Then
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
Exit Try
End If
End Try
End If
End Sub