Posted Forms Code
httpWebRequest.aspx
1 2 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="httpWebRequest.ascx.vb" Inherits="DamianM.httpWebRequest3" %> <asp:Literal id="Literal1" runat="server"></asp:Literal> |
httpWebRequest.aspx.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | Public Class httpWebRequest3 Inherits System.Web.UI.UserControl Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim req As System.Net.HttpWebRequest Dim res As System.Net.HttpWebResponse Dim sr As System.IO.StreamReader Dim s As System.IO.Stream Dim b As Byte() '--notice that the instance is created using webrequest '--this is what microsoft recomends req = System.Net.WebRequest.Create("http://www.damianm.co.uk/code/4.3/formreader.aspx") 'enncode the form data string into a byte array b = System.Text.Encoding.ASCII.GetBytes("text=test text&password=secret&checkbox=on&textarea=a longer text sentence&submit=submit") 'indicate that you will be posting the data req.Method = "post" req.ContentType = "application/x-www-form-urlencoded" 'send the form req.ContentLength = b.Length s = req.GetRequestStream() s.Write(b, 0, b.Length) res = req.GetResponse() 'read in the page sr = New System.IO.StreamReader(res.GetResponseStream()) Literal1.Text = sr.ReadToEnd '--tidy up sr.Close() res.Close() End Sub End Class |
Webclient.aspx
1 2 | <%@ Control Language="vb" AutoEventWireup="false" Codebehind="Webclient.ascx.vb" Inherits="DamianM.Webclient3" %> <asp:Literal id="Literal1" runat="server"></asp:Literal> |
Webclient.aspx.vb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Public Class Webclient3 Inherits System.Web.UI.UserControl Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim wc As New System.Net.WebClient() Dim b As Byte() Dim res As Byte() 'enncode the form data string into a byte array b = System.Text.Encoding.ASCII.GetBytes("text=test text&password=secret&checkbox=on&textarea=a longer text sentence&submit=submit") 'set the content type of the data wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded") 'scrapte the data res = wc.UploadData("http://www.damianm.co.uk/code/4.3/formreader.aspx", b) 'convert the return page from a byte array to ascii and display Literal1.Text = System.Text.Encoding.ASCII.GetString(res) End Sub End Class |



















