Logge ind vha httpWebRequest og redirect
HejJeg er ved at lave et lille program der skal holde øje med status på en webside for mig.
Kort beskrevet er jeg ved at lave et program der logger ind på en webside. Når jeg logger ind på den første side er der i html'en der retuneres et <meta> tag som redirecter mig til en ny side. Den URL kan jeg godt fange men herefter går det galt. Serveren der modtager mit svar sender en advarsel tilbage.
Jeg tror det er mine cookies der er noget galt med men er ikke sikker.
Håber i kan hjæælpe.
Dim UserID As String = "aldius" ' Username
Dim PWord As String = "password" ' Password
Dim domain As String = "http://s2.gladiatus.dk/game/index.php?mod=login"
Dim encoding As New System.Text.ASCIIEncoding
Dim CookieC As New Net.CookieContainer
Dim HTML As String
Dim LoginReq As Net.HttpWebRequest
Dim SendReq As IO.Stream
Dim LoginRes As Net.HttpWebResponse
Dim sReader As IO.StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Use the appropriate HTML field names to stuff into the post header
Dim PostData As String = _
"user=" & UserID & _
"&pass=" & PWord ' Note: where txtUName & txtPWord are the field names
Dim Data() As Byte = encoding.GetBytes(PostData)
' Initialise the request
LoginReq = Net.WebRequest.Create(domain) ' Login location taken from the form action
With LoginReq
.KeepAlive = True
.Method = "POST"
.AllowAutoRedirect = False
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = Data.Length
.Referer = "http://s2.gladiatus.dk/game/index.php?mod=login"
.CookieContainer = CookieC
End With
' Add the POST data
SendReq = LoginReq.GetRequestStream
SendReq.Write(Data, 0, Data.Length)
SendReq.Close()
' Obtain the response
LoginRes = LoginReq.GetResponse()
sReader = New IO.StreamReader(LoginRes.GetResponseStream)
HTML = sReader.ReadToEnd
Dim tstr As String
tstr = HTML.Substring(HTML.IndexOf("URL"))
tstr = tstr.Substring(4)
tstr = "http://s2.gladiatus.dk/game/" + tstr.Substring(0, tstr.IndexOf("><"))
LoginReq = Net.WebRequest.Create(tstr) ' Login location taken from the form action
With LoginReq
.KeepAlive = True
.Method = "GET"
End With
' Obtain the response
LoginRes = Nothing
LoginRes = LoginReq.GetResponse()
sReader = New IO.StreamReader(LoginRes.GetResponseStream)
HTML = sReader.ReadToEnd
end sub