Opening source via code

grags

Active member
Joined
Mar 26, 2005
Messages
26
Programming Experience
Beginner
Ok, i'm not sure if i'm in the right forum here? If not i'm sorry and can an admin or moderator move it to the right place... thankyou


Anyway here's my problem.

If I right click on a web page I can view source.
Is there a way to do this via code?

Example: FileOpen(1, WebPage, OpenMode.Input)

So basically it opens the web page as a text file. That way I can simply search through the text and pull out the information (using code).


Any help at all would be greatly appreciated. If it's not clear what I'm asking, please please tell me so I can make it a clearer


Thankyou


Grags_1977
Graham Maddocks
 
WebClient class in System.Net namespace has got a DownloadString method you can use. Example:
VB.NET:
Dim web As New Net.WebClient
Dim str As String = web.DownloadString("http://webaddress.net")
There are several methods to save the string to a file. If you use the 'Insert snippet' feature it will give you this single line of code to do it:
VB.NET:
My.Computer.FileSystem.WriteAllText("C:\Test.txt", str, True)
What happens when you 'view source' in IE browser is that the text is saved to a temporary file in Internet cache folder and opened in Notepad.
 
Thankyou for your reply. I really appreciate it. But it isn't working?

It doesn't recognize DownloadString

If it's of any use to you im using VB.Net 2003
 
Then you should change Primary Platform in your user profile to .Net 1.1 or state the version of your request to avoid such confusion. Thread was also moved from WebServices forum to Net&Sockets forum.

You can still use WebClient, but are limited to the DownloadData method and have to convert the returned byte array to string. Example:
VB.NET:
Dim web As New Net.WebClient
Dim str As String = System.Text.Encoding.Default.GetString(web.DownloadData("http://webaddress.net")
Another alternative is to dig a little deeper with the web requests and responses and working some with streams, example:
VB.NET:
Dim myWebRequest As Net.WebRequest = Net.WebRequest.Create([URL="http://webaddress.net/"]http://webaddress.net[/URL])
Dim myWebResponse As Net.WebResponse = myWebRequest.GetResponse()
Dim sr As New IO.StreamReader(myWebResponse.GetResponseStream)
Dim str As String = sr.ReadToEnd
sr.Close()
Depending on how you search and handle the content, there may be a benefit in accessing the document object through the DOM nodes. To do this use the WebBrowser control and navigate it to the url, then do lookups with the methods of HtmlDocument instance in WebBrowser.Document property. See this thread (examples both for .Net 2 and 1 there actually, same problem with user platform..) http://www.vbdotnetforums.com/showthread.php?t=11910&highlight=webbrowser
 
Back
Top