V
VS
Hi
I am trying to access a web application from another program using
HttpWebRequest class. This web application is nothing but a web site
consisting of a login page and few other pages. I'm trying to access
the login page and simulate a login action programatically. I am
running the web application on debug mode so when I send in a request
I can step through to see if the data I have posted is available to
the login page. The text boxes are web controls and hence the code
does this - txtUsr.Value... to get the value from an input box. The
data I have posted in available in the request objects params
collection and hence you can access it only by doing
request.params("txtUsr") and not in like txtUsr.Value, this way it
return an empty string. Please can someone help with this problem
where I can send data and the web application can access the
appropriate input box values by doing txtUsr.Value.
Dim URL As String = "http://myURL/logon.aspx?"
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(URL),
HttpWebRequest)
myHttpWebRequest.Method = "POST"
Dim postData As String = "txtUsr=myUsername&txtPwd=myPassword"
Dim encodedData As New ASCIIEncoding()
Dim byteArray As Byte() = encodedData.GetBytes(postData)
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
myHttpWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
newStream.Close()
myHttpWebRequest.AllowAutoRedirect = True
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
'read response
Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
' Pipes the response stream to a higher level stream reader with the
required encoding format.
Dim readStream As New StreamReader(receiveStream, encode)
Dim read(256) As [Char]
' Reads 256 characters at a time.
Dim count As Integer = readStream.Read(read, 0, 256)
Dim tempStr As String
While count > 0
Dim str As New [String](read, 0, count)
tempStr = tempStr & str
count = readStream.Read(read, 0, 256)
End While
Return tempstr
I am trying to access a web application from another program using
HttpWebRequest class. This web application is nothing but a web site
consisting of a login page and few other pages. I'm trying to access
the login page and simulate a login action programatically. I am
running the web application on debug mode so when I send in a request
I can step through to see if the data I have posted is available to
the login page. The text boxes are web controls and hence the code
does this - txtUsr.Value... to get the value from an input box. The
data I have posted in available in the request objects params
collection and hence you can access it only by doing
request.params("txtUsr") and not in like txtUsr.Value, this way it
return an empty string. Please can someone help with this problem
where I can send data and the web application can access the
appropriate input box values by doing txtUsr.Value.
Dim URL As String = "http://myURL/logon.aspx?"
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(URL),
HttpWebRequest)
myHttpWebRequest.Method = "POST"
Dim postData As String = "txtUsr=myUsername&txtPwd=myPassword"
Dim encodedData As New ASCIIEncoding()
Dim byteArray As Byte() = encodedData.GetBytes(postData)
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
myHttpWebRequest.ContentLength = byteArray.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
newStream.Close()
myHttpWebRequest.AllowAutoRedirect = True
Dim myHttpWebResponse As HttpWebResponse =
CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
'read response
Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
' Pipes the response stream to a higher level stream reader with the
required encoding format.
Dim readStream As New StreamReader(receiveStream, encode)
Dim read(256) As [Char]
' Reads 256 characters at a time.
Dim count As Integer = readStream.Read(read, 0, 256)
Dim tempStr As String
While count > 0
Dim str As New [String](read, 0, count)
tempStr = tempStr & str
count = readStream.Read(read, 0, 256)
End While
Return tempstr