|
-
May 20th, 2007, 10:26 PM
#1
Thread Starter
Lively Member
[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.
-
May 20th, 2007, 10:34 PM
#2
Fanatic Member
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
-
May 20th, 2007, 11:29 PM
#3
Thread Starter
Lively Member
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
-
May 20th, 2007, 11:34 PM
#4
Thread Starter
Lively Member
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
-
May 20th, 2007, 11:38 PM
#5
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:
Using wc As New WebClient
IO.File.WriteAllText("file path here", wc.DownloadString("URL here"))
End Using
-
May 20th, 2007, 11:45 PM
#6
Thread Starter
Lively Member
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
-
May 20th, 2007, 11:57 PM
#7
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:
Dim wc As New WebClient
Dim sr As New StreamWriter("file path here")
sr.Write(wc.DownloadString("URL here")
sr.Dispose()
wc.Dispose()
-
May 21st, 2007, 12:03 AM
#8
Thread Starter
Lively Member
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?
-
May 21st, 2007, 12:10 AM
#9
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.
-
May 21st, 2007, 12:20 AM
#10
Thread Starter
Lively Member
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
-
May 21st, 2007, 12:26 AM
#11
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:
Dim wc As New WebClient
wc.DownloadFile("URL here", "file path here")
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.
-
May 21st, 2007, 12:35 AM
#12
Thread Starter
Lively Member
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
-
May 21st, 2007, 01:18 AM
#13
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?
-
May 21st, 2007, 01:20 AM
#14
Thread Starter
Lively Member
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?
-
May 21st, 2007, 01:29 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|