HttpWebRequest

  • Thread starter Thread starter Shawn
  • Start date Start date
S

Shawn

Hi.
I'm trying to post a form from my code.
I've created a simple page (page_2.asp):

<html>
<body>
<%
response.write(request.form("username"))
%>
</body>
</html>

Now, here is the code i'm using to try and post to this page:

Dim req As HttpWebRequest
Dim resp As WebResponse
Dim data() As Byte
Dim form_post_data As String
Dim encoding As System.Text.ASCIIEncoding = New
System.Text.ASCIIEncoding
Dim sr As IO.StreamReader

form_post_data = "&username=" & "test" & "&password=" & "test"

data = encoding.GetBytes(form_post_data)
req = CType(WebRequest.Create("http://localhost/tmp/asp/page_2.asp"),
HttpWebRequest)

Try
req.Method = "POST"
req.KeepAlive = True
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = data.Length

Dim newStream As IO.Stream
newStream = req.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()

resp = CType(req.GetResponse(), HttpWebResponse)
sr = New IO.StreamReader(resp.GetResponseStream())

Dim result As String = sr.ReadToEnd()
sr.Close()
resp.Close()
Catch ex As Exception

End Try

result should contain:
<html>
<body>
test

</body>
</html>
But instead it just returns the html and body tag. What am I doing wrong
here?

Any help is greatly appreciated!!

Shawn
 
Hi Shawn,

I think that there is no problem in your .NET code. The problem might be the
asp page can't get form data. You might try to use a .NET page to receive
form data.

HTH

Elton Wang
(e-mail address removed)
 
Shawn said:
Hi.
I'm trying to post a form from my code.
I've created a simple page (page_2.asp):

<html>
<body>
<%
response.write(request.form("username"))
%>
</body>
</html>

Now, here is the code i'm using to try and post to this page:

Dim req As HttpWebRequest
Dim resp As WebResponse
Dim data() As Byte
Dim form_post_data As String
Dim encoding As System.Text.ASCIIEncoding = New
System.Text.ASCIIEncoding
Dim sr As IO.StreamReader

form_post_data = "&username=" & "test" & "&password=" & "test"

data = encoding.GetBytes(form_post_data)
req =
CType(WebRequest.Create("http://localhost/tmp/asp/page_2.asp"),
HttpWebRequest)

Try
req.Method = "POST"
req.KeepAlive = True
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = data.Length

Dim newStream As IO.Stream
newStream = req.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()

resp = CType(req.GetResponse(), HttpWebResponse)
sr = New IO.StreamReader(resp.GetResponseStream())

Dim result As String = sr.ReadToEnd()
sr.Close()
resp.Close()
Catch ex As Exception

End Try

result should contain:
<html>
<body>
test

</body>
</html>
But instead it just returns the html and body tag. What am I doing
wrong here?

Any help is greatly appreciated!!

Don't use ASCIIEncoding for encoding form data. This is almost always
wrong, unless you're only using English language content.

You're decoding the result page using a default StreamReader (UTF-8).
This might be wrong as well -- if the web site uses an 8 bit encoding
like ISO-8859-x, the response will be corrupted.


Cheers,
 
Back
Top