Sending Request And Get Response with POST Method

February 3, 2009
Imports System.IO  
Imports System.Net  
Partial Class Default1  
    Inherits System.Web.UI.Page  
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        Dim strURL As String = “”  
        Dim strPostData As String = “”  
        Dim strResult As String = “”  
        Dim wbrq As HttpWebRequest  
        Dim wbrs As HttpWebResponse  
        Dim sw As StreamWriter  
        Dim sr As StreamReader  
  
        ’ Set the URL to post to  
        strURL = “http://www.webcom.com/cgi-bin/form”  
        ’ Post some values to the page  
        strPostData = String.Format(“your_name={0}&userid={1}&form_name={2}”, “Mark Smith”, “webcom”, “tutortest”)  
  
        ’ Create the web request  
        wbrq = WebRequest.Create(strURL)  
        wbrq.Method = “POST”  
        ’ We don’t always need to set the Referer but in this case   
        ’ the page we are posting to will only issue a response if we do  
        wbrq.Referer = “http://www.webcom.com/cgi-bin/form”  
        wbrq.ContentLength = strPostData.Length  
        wbrq.ContentType = “application/x-www-form-urlencoded”  
  
        ’ Post the data  
        sw = New StreamWriter(wbrq.GetRequestStream)  
        sw.Write(strPostData)  
        sw.Close()  
  
        ’ Read the returned data   
        wbrs = wbrq.GetResponse  
        sr = New StreamReader(wbrs.GetResponseStream)  
        strResult = sr.ReadToEnd.Trim  
        sr.Close()  
  
        ’ Write the returned data out to the page  
        TextBox1.Text = strResult  
    End Sub  
End Class