HttpWebRequest

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
 
G

Guest

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

Joerg Jooss

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,
 

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

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top