Results 1 to 15 of 15

Thread: [02/03] Retrieving files using HTTP, then converting file to .txt format

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    [02/03] Retrieving files using HTTP, then converting file to .txt format

    Hi everyone, I'd like to know if its possible in VB to visit a webpage, then save the webpage from a .asp to a .txt format.

    And if it's possible, can it be done hidden too? So the user doesnt see the visiting of the webpage and all, the user just sees the end result of the .txt version of the webpage in the folder on the deskstop or something..
    Last edited by Eddd88; May 20th, 2007 at 11:38 PM.

  2. #2
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [02/03] Is there a way to visit a webpage and save the webpage from .asp to .txt

    The following code will save the page source of a given webpage to a string.

    Code:
    Dim request As WebRequest = WebRequest.Create("http://www.vbforums.com/")
    Dim response As WebResponse = request.GetResponse
    Dim stream As System.IO.Stream = request.GetResponse.GetResponseStream
    Dim streamReader As System.IO.StreamReader = New System.IO.StreamReader(stream)
    String pageSource = streamReader.ReadToEnd

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Is there a way to visit a webpage and save the webpage from .asp to .txt

    Alright, so it saves the webpage down, can it save the webpage in a different file format too? like if i'm not wrong most webpages are saved in .asp (or at least in my case it is), and i need it in a .txt format

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Is there a way to visit a webpage and save the webpage from .asp to .txt

    And er, is the code you gave in VS 2003? 'cause I've just tried it and got the wiggles under the WebRequest, WebResponse and String pageSource..

    For the WebRequest and WebResponse, it says they're not declared, so do I declare them as Strings too or something?

    For the String, it says 'String' is a class type and so is not a valid expression.

    And lastly for the pageSource, it says '.' is expected

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Use the WebClient.DownloadString method. You specify the URL and it returns the output as a String, e.g.
    vb.net Code:
    1. Using wc As New WebClient
    2.     IO.File.WriteAllText("file path here", wc.DownloadString("URL here"))
    3. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    I created a button on a winform and pasted the code you posted jmcilhinney in the button and it gave wiggles for Using wc As, IO.File.WriteAllText and End

    For Using and wc it says they aren't declared

    For As it says Comma, ')', or a valid expression continuation is needed

    For IO.File.WriteAllText it says WriteAllText is not a member of 'System.IO.File'..so i thought it's because i didn't import System.IO, but after i did, the wiggle still remains

    And the 'End', it says 'End' statement not valid

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Ah, bummer. I missed that this is .NET 1.x.
    vb.net Code:
    1. Dim wc As New WebClient
    2. Dim sr As New StreamWriter("file path here")
    3.  
    4. sr.Write(wc.DownloadString("URL here")
    5.  
    6. sr.Dispose()
    7. wc.Dispose()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Thats strange, VS 2003 refuses to give me WebClient

    As in when i type Dim wc As New then i press the spacebar and that list of stuff i can use pops up, it doesnt list WebClient as one of them, and i forcefully type it in and it gives the wiggles saying Type 'WebClient' not defined =/

    Is this only usable in VS 2005 sadly?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    The WebClient class has existed since .NET 1.0. Just like any other class, you have to either qualify it with its namespace or else import its namespace. If you read the documentation for any class in the MSDN library it will tell you which namespace and which assembly a type is declared in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Okays, so I had to import System.Net

    and then the wiggles came off from the WebClient in

    Dim wc As New WebClient

    but the wiggle remained for wc.DownloadString in

    sr.Write(wc.DownloadString("URL here")

    the wiggle says that 'DownloadString' is not a member of 'System.Net.WebClient'

    so when i added a WebClient to the System.Net that i already imported, it takes away the wiggle from wc.DownloadString but then it gives a wiggle for WebClient in

    Dim wc As New WebClient

    saying that Type 'WebClient' is not defined

    so i added another import line to import System.Net back and when i did that, now WebClient has no wiggles but wc.DownloadString has a wiggle again, and now i have both System.Net and System.Net.WebClient imported

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Man, I really botched this one didn't I. DownloadString wasn't added until .NET 2.0. The DownloadFile method has existed from the start though. If it works it would actually be better because there's no need for a StreamWriter. I'm just not sure whether the URL you supply has to refer to an existing file or it can refer to variable output. Hopefully the latter, in which case this will do:
    vb.net Code:
    1. Dim wc As New WebClient
    2.  
    3. wc.DownloadFile("URL here", "file path here")
    4. wc.Dispose()
    If that doesn't work then go back to what JC suggested and forget I was ever here. Better yet, upgrade to VS 2005 and make my life easier.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Well now i got something like this

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    		Dim wc As New WebClient
    		wc.DownloadFile("http://www.yahoo.com", "C:\Documents and Settings\jy\My Documents\Visual Studio Projects\WindowsApplication8")
    		wc.Dispose()
    	End Sub
    Unfortunately, an error keeps coming up saying

    Additional information: An exception occured during a WebClient request

    I guess thats because what you said was right that the URL i had to provide would either refer to an existing file or it can refer to a variable output, and i think that it seems to want to refer to an existing file

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    Look at what I posted:
    Code:
    wc.DownloadFile("URL here", "file path here")
    Now look at what you posted:
    Code:
    wc.DownloadFile("http://www.yahoo.com", "C:\Documents and Settings\jy\My Documents\Visual Studio Projects\WindowsApplication8")
    Is that a file path?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    81

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    i thought file path means the place where i want the file (url) to get stored?

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Retrieving files using HTTP, then converting file to .txt format

    The file path means the path of the file. You've supplied the path of the folder you want to save the file under.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width