Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to open an URL and save the content?

how to open an URL and save the content?

Scheduled Pinned Locked Moved Visual Basic
csharphtmlsysadmintoolstutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kevindotnet
    wrote on last edited by
    #1

    Is there any way to make a .net webpage, and in the server script to grab the URL and save the content to actual HTML file?

    M D 2 Replies Last reply
    0
    • K kevindotnet

      Is there any way to make a .net webpage, and in the server script to grab the URL and save the content to actual HTML file?

      M Offline
      M Offline
      MidwestLimey
      wrote on last edited by
      #2

      Simple answer: yes. In the code behind you will need to utilize a component (or write one) that makes the http request to the server running the .net webform and cache the response. The simplest mechanism would be to open a socket to port 80 (unless you're running https) of the server, send the GET or POST request and cache the response. You'll need to strip off the HTTP headers. eg. send: GET /some/dotnet/file.aspx HTTP/1.1 check the reponse contains: HTTP/1.1 200 OK in the first line. To strip off the HTTP headers in the response, look for the first empty line. The HTML content should be after that.


      I'm largely language agnostic


      After a while they all bug me :doh:


      1 Reply Last reply
      0
      • K kevindotnet

        Is there any way to make a .net webpage, and in the server script to grab the URL and save the content to actual HTML file?

        D Offline
        D Offline
        DigiOz Multimedia
        wrote on last edited by
        #3

        Here is a function that lets you do that: Public Function GetWebPageResult(ByVal webPG As String) As String Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(webPG), HttpWebRequest) Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse) Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream() Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8") Dim readStream As New StreamReader(receiveStream, encode) GetWebPageResult = readStream.ReadToEnd() ' Use this 'readStream' where ever you want. End Function Just call this function and pass the webpage URL to it. It will grab the html content of the page and return it as a string as the result of the function.

        Pete Soheil DigiOz Multimedia http://www.digioz.com

        L M K 3 Replies Last reply
        0
        • D DigiOz Multimedia

          Here is a function that lets you do that: Public Function GetWebPageResult(ByVal webPG As String) As String Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(webPG), HttpWebRequest) Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse) Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream() Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8") Dim readStream As New StreamReader(receiveStream, encode) GetWebPageResult = readStream.ReadToEnd() ' Use this 'readStream' where ever you want. End Function Just call this function and pass the webpage URL to it. It will grab the html content of the page and return it as a string as the result of the function.

          Pete Soheil DigiOz Multimedia http://www.digioz.com

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, IMO you should Close both the WebResponse and the Stream inside your function. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          D 1 Reply Last reply
          0
          • D DigiOz Multimedia

            Here is a function that lets you do that: Public Function GetWebPageResult(ByVal webPG As String) As String Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(webPG), HttpWebRequest) Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse) Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream() Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8") Dim readStream As New StreamReader(receiveStream, encode) GetWebPageResult = readStream.ReadToEnd() ' Use this 'readStream' where ever you want. End Function Just call this function and pass the webpage URL to it. It will grab the html content of the page and return it as a string as the result of the function.

            Pete Soheil DigiOz Multimedia http://www.digioz.com

            M Offline
            M Offline
            MidwestLimey
            wrote on last edited by
            #5

            I'd forgotten about that class, cheers. Been working on winforms too long!


            I'm largely language agnostic


            After a while they all bug me :doh:


            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, IMO you should Close both the WebResponse and the Stream inside your function. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              D Offline
              D Offline
              DigiOz Multimedia
              wrote on last edited by
              #6

              Luc Pattyn wrote:

              IMO you should Close both the WebResponse and the Stream inside your function.

              LOL... oops.... my bad. :) Yeah, make sure to close both those.

              Pete Soheil DigiOz Multimedia http://www.digioz.com

              1 Reply Last reply
              0
              • D DigiOz Multimedia

                Here is a function that lets you do that: Public Function GetWebPageResult(ByVal webPG As String) As String Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(webPG), HttpWebRequest) Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse) Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream() Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8") Dim readStream As New StreamReader(receiveStream, encode) GetWebPageResult = readStream.ReadToEnd() ' Use this 'readStream' where ever you want. End Function Just call this function and pass the webpage URL to it. It will grab the html content of the page and return it as a string as the result of the function.

                Pete Soheil DigiOz Multimedia http://www.digioz.com

                K Offline
                K Offline
                kevindotnet
                wrote on last edited by
                #7

                Hi Digi, thatnks for the reply. The function is awesome. it works like charm ^^ BTW, i just find out another solution, to share with you guys here =D I used webclient class, there is a readopen() method to achieve this. here is a sample class which i get from msdn. Public Class Test Public Shared Sub Main(args() As String) If args Is Nothing OrElse args.Length = 0 Then Throw New ApplicationException("Specify the URI of the resource to retrieve.") End If Dim client As New WebClient() ' Add a user agent header in case the ' requested URI contains a query. client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") Dim data As Stream = client.OpenRead(args(0)) Dim reader As New StreamReader(data) Dim s As String = reader.ReadToEnd() Console.WriteLine(s) data.Close() reader.Close() End Sub 'Main End Class 'Test :-D

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups