-2

I am using Visual Basic 2010 Express and have found a way to read a file:

Dim byter = My.Computer.FileSystem.ReadAllBytes("C:/Documents and Settings/textfile.txt")

Can I do something similar if I want to read the contents of a website?

  • What you want to read a content of webpage or file like archive, image and so on? Either way you need to establish connection, set content type, read bytes and so on – Mark Zucchini Jan 24 '15 at 12:42

1 Answers1

0

Try something like:

  Dim uri as New Uri("http://thewebsite")
  Dim request as HttpWebRequest = HttpWebRequest.Create(uri)
  request.Method = WebRequestMethods.Http.Get
  Dim response As HttpWebResponse = request.GetResponse()
  Dim reader As New StreamReader(response.GetResponseStream())
  Dim webpageContents As String = reader.ReadToEnd()
  response.Close()
JustinHui
  • 729
  • 5
  • 18
  • I got some errors and the compiler suggested some corrections: Dim uri As New Uri("http://thewebsite") Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(uri) Then it worked, so thanks! request.Method = Net.WebRequestMethods.Http.Get Dim response As Net.HttpWebResponse = request.GetResponse() Dim reader As New IO.StreamReader(response.GetResponseStream()) Dim webpageContents As String = reader.ReadToEnd() response.Close() – larsemanse Jan 24 '15 at 14:20
  • The compiler suggested changes so I tried: Dim uri as New Uri("http://thewebsite") Dim request as Net.HttpWebRequest = Net.HttpWebRequest.Create(uri) request.Method = Net.WebRequestMethods.Http.Get Dim response As Net.HttpWebResponse = request.GetResponse() Dim reader As New IO.StreamReader(response.GetResponseStream()) Dim webpageContents As String = reader.ReadToEnd() response.Close() Then it worked, so thanks! – larsemanse Jan 24 '15 at 14:30
  • It worked after some small changes, sorry for the bad editing. – larsemanse Jan 24 '15 at 14:32
  • After viewing the downloaded string I found out it was the html code for the page that was downloaded. What I really need is the content of the page, like I would get if I marked the whole page and copied it and then pasted it into a text editor. – larsemanse Jan 24 '15 at 15:19
  • I found a way to use the content of the downloaded string, please disregard the question in my last comment. – larsemanse Jan 24 '15 at 15:56
  • Have a look at the answers to this question http://stackoverflow.com/questions/4377355/i-need-a-powerful-web-scraper-library – JustinHui Jan 24 '15 at 15:56