Im in need of downloading the HTML generated by a server after sending a URL. I thought maybe the webclient.downloadfile would work but it seems like it gives up when you don't end the URL with a file extension.
If I do have a webbrowser control set up that has navigated to a site on the same domain;however, that webbrowser has a handler set up for the documentcompleted event that I do not want to fire.
If the best solution is to load the page and use the webbrowser.documentstream to get a stream and write it to a file I have 2 questions:
1) the document.stream returns a system.io.stream and the file object uses system.io.filestream. Can I easily write a stream to a filestream and if so, how?
2) I answered this question for myself when I was typing it out :P
If this is not the best solution and the webclient can infact download a file after it's generated, how would I go about that?\
Edit: If I'm way off base, don't hesitate to let me know :) I'm still quite new to VB
edit: The below is not working. My step through revealed that I am in fact passing usable data to the functions, but for some reason the files are still not being created.
Uniencoding declaration:
Dim uniencoding As New System.Text.UnicodeEncoding()
Code:
request = WebRequest.Create(tempstring)
Using response As WebResponse = request.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
Dim html As String = reader.ReadToEnd()
fstream = New FileStream(surl, FileMode.Create)
fstream.Write(uniencoding.GetBytes(html), 0, Len(uniencoding.GetBytes(html))) 'Write the HTML to a local file
End Using
End Using
If all you want to do is get the HTML (or whatever the response contains), without actually rendering it to the screen, you can do that easily using the WebRequest
class.
Dim request As WebRequest = WebRequest.Create("http://www.google.com")
using response As WebResponse = request.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
Dim html As String = reader.ReadToEnd()
File.WriteAllText("test.html", html)
End Using
End Using