Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Server Side HTTP request


Message #1 by Jagdeep Dua <Jagdeep.Dua@n...> on Mon, 21 Oct 2002 23:09:21 +0100
Is there a way to do a server side http post/get such that if I execute one,
I can get the response into some text stream of some sort?

Your help appreciated.

Thanks
Jagdeep Dua
Message #2 by "Mingkun Goh" <mangokun@h...> on Fri, 25 Oct 2002 10:43:27
Found & taken from:
.NET 247 : ngfx-io - sending http post request
http://www.dotnet247.com/247reference/msgs/19/95431.aspx


    'The function below takes an absolute URL and parameters in the form 
of "param1=blah&m2=bloh", makes a POST request, and returns the resulting 
HTML page as a string.
    Function readHtmlPage(ByVal url As String, ByVal params As String) As 
String
        Dim result As String = ""
        Dim myWriter As StreamWriter

        Dim objRequest As HttpWebRequest = WebRequest.Create(url)
        objRequest.Method = "POST"
        objRequest.ContentLength = params.Length
        objRequest.ContentType = "application/x-www-form-urlencoded"
        objRequest.KeepAlive = False

        Try
            myWriter = New StreamWriter(objRequest.GetRequestStream())
            myWriter.Write(params)
        Catch e As Exception
            Return e.Message
        Finally
            myWriter.Close()
        End Try

        Dim objResponse As HttpWebResponse = objRequest.GetResponse()
        Dim sr As StreamReader
        sr = New StreamReader(objResponse.GetResponseStream())
        result = sr.ReadToEnd()
        sr.Close()

        myWriter.Close()

        Return result
    End Function


    'The below code I created demostrates how to use the above function to 
send a POST request to an URL/page
    Public Function PostPageRequest() As Boolean
        Dim str1 As String

        Try
            'First you have to identify the form field(s) together with 
their values that will be sent to the resulting URL/page

            'the readHtmlPage function will return a string which is the 
page from the POST request
            str1 = readHtmlPage("https://www.income.com.sg/i-
shop/category.asp", "category=Promotions+%26+Specials")

            'then you may continue to process the resulting page from the 
post request

            Return True
        Catch
            Return False
        End Try
    End Function



Previous message:

> Is there a way to do a server side http post/get such that if I execute 
one,
I can get the response into some text stream of some sort?

Your help appreciated.

Thanks
Jagdeep Dua
Message #3 by "Mingkun Goh" <mangokun@h...> on Fri, 25 Oct 2002 10:53:44
Sending a HTTP GET request is simply constructing the page URL string with 
the necessary parameters appended to it.
e.g. 
http://localhost/test.aspx?param1=value1&param2=value2
or
http://p2p.wrox.com/post.asp?list=aspx&id=226765


Here is a 
  <name>ScreenScrape Function</name>
that will
  <description>Scrape an page off the Internet and returns it as a 
string</description>

    Public Function ScreenScrape(ByVal remoteURL As String) As String
        Try
            ' Create a new WebClient instance.
            Dim myWebClient As New System.Net.WebClient()

            ' DownloadData() method takes a 'UriRemote' and downloads the 
Web resource and saves it into a data buffer.
            Dim myDatabuffer As Byte() = myWebClient.DownloadData
(remoteURL)

            Return System.Text.Encoding.ASCII.GetString(myDatabuffer)
        Catch e As Exception
            Return e.Message
        End Try
    End Function



Using it is as simple as passing it with an URL string.
e.g. 
str1 = ScreenScrape("http://www.iss.nus.edu.sg/iss/event_calendar.jsp?
type=sc")




Previous message:

> Is there a way to do a server side http post/get such that if I execute 
one,
I can get the response into some text stream of some sort?

Your help appreciated.

Thanks
Jagdeep Dua

  Return to Index