http_post.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim strDataToPost As String
Dim myWebRequest As WebRequest
Dim myRequestStream As Stream
Dim myStreamWriter As StreamWriter
Dim myWebResponse As WebResponse
Dim myResponseStream As Stream
Dim myStreamReader As StreamReader
' Create a new WebRequest which targets that page with
' which we want to interact.
myWebRequest = WebRequest.Create( _
"http://www.asp101.com/samples/http_post_target.asp")
' Set the method to "POST" and the content type so the
' server knows to expect form data in the body of the
' request.
With myWebRequest
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
End With
' Here's the data I'll be sending. It's the same format
' you'd use in a querysting and you should URLEncode any
' data that may need it.
strDataToPost = "Name=John&Date=" & Server.UrlEncode(Now())
' Get a handle on the Stream of data we're sending to
' the remote server, connect a StreamWriter to it, and
' write our data to the Stream using the StreamWriter.
myRequestStream = myWebRequest.GetRequestStream()
myStreamWriter = New StreamWriter(myRequestStream)
myStreamWriter.Write(strDataToPost)
myStreamWriter.Flush()
myStreamWriter.Close()
myRequestStream.Close()
' Get the response from the remote server.
myWebResponse = myWebRequest.GetResponse()
' Get the server's response status?
' Just like when we sent the data, we'll get a reference
' to the response Stream, connect a StreamReader to the
' Stream and use the reader to actually read the reply.
myResponseStream = myWebResponse.GetResponseStream()
myStreamReader = New StreamReader(myResponseStream)
litResponseText.Text = myStreamReader.ReadToEnd()
myStreamReader.Close()
myResponseStream.Close()
' Close the WebResponse
myWebResponse.Close()
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET HTTP Post Request Sample from ASP 101</title>
</head>
<body>
<form id="myForm" runat="server">
<p>
<strong>This page made an HTTP post request to:</strong><br />
<code>http://www.asp101.com/samples/http_post_target.asp</code>
</p>
<p>
<strong>It sent two pieces of data:</strong><br />
<code>Name</code> and <code>Date</code>
</p>
<p>
<strong>The Response:</strong>
</p>
<table border="1">
<tr><td>
<asp:Literal ID="litResponseText" runat="server" />
</td></tr>
</table>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/http_post_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>