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),

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)

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

Natty Gur


It quit tricky one.

ASP.NET need __VIEWSTATE field (even with empty value) to simulate
postback, create controls and set form values. The problem is that if
you post __VIEWSTATE data with your form to the server you will get
error because the __VIEWSTATE data can't be parsed by the calling page.

if you will override SavePageStateToPersistenceMedium and
LoadPageStateFromPersistenceMedium methods on your calling page to
disable default view state process and add __VIEWSTATE='' to your form
data you will get those values that you send inside page textbox's.

protected override void SavePageStateToPersistenceMedium(object

protected override object LoadPageStateFromPersistenceMedium()
return null;

I don't know if I help you to solve your problems but I hope you have
good explanation way it happened.

Natty Gur[MVP]

blog :
Mobile: +972-(0)58-888377

Natty Gur


In a second throughout the problem is due to the fact that
LoadPageStateFromPersistenceMedium is trying to read a value that didn't
write yet (usually first call to server writes viewstate and second
call read it). So if you override LoadPageStateFromPersistenceMedium in
such a way that if base.LoadPageStateFromPersistenceMedium failed the
process continues, ASP.NET will continue to load controls and set their

Thanks for this Interesting post!

protected override object LoadPageStateFromPersistenceMedium()
object oViewState = null;
oViewState = base.LoadPageStateFromPersistenceMedium();
catch(Exception HttpException)

return oViewState;
Natty Gur[MVP]

blog :
Mobile: +972-(0)58-888377

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question