ARTICLE

Download contents of a Web Page through HTTP

Posted by Mahesh Chand Articles | Visual Basic 2010 June 25, 2003
CSDownloadURL is a class which has two functions - SetURL and DownloadURL. The set URL sets the current URL and GetDownload downloads the URL contents and returns its contents in a string.
 
Reader Level:

CSDownloadURL is a class, which has two functions - SetURL and DownloadURL. The set URL sets the current URL and GetDownload downloads the URL contents and returns its contents in a string.

Note: This class was written in Beta 1. In recent .NET versions, the WebRequestFactory class is no longer valid. This is now updated to RC3.

You can use the WebRequest class to download the contents of a web page. The WebRequest uses HTTP protocol to download the contents. You use Create method of WebRequest to create a WebRequest object by passing a URL as a parameter.

After that you can call GetResponse and GetResponseStream methods. The GetResponseStream method returns data in a Stream object.

Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Namespace Project5
' <summary>
' Summary description for Class1.
' </summary>
Public Class CSDownloadURL
Private m_strURL As String
Public
Sub setURL1(strURL As String)
m_strURL = strURL
End Sub 'setURL1
Public Sub DownloadURL(ByRef strContents As String)
Dim req As WebRequest = WebRequest.Create(m_strURL)
Dim res As WebResponse = req.GetResponse()
Dim iTotalBuff As Integer = 0
Dim Buffer(128) As [Byte]
Dim stream As Stream = res.GetResponseStream()
iTotalBuff = stream.Read(Buffer, 0, 128)
Dim strRes As New StringBuilder("")
While iTotalBuff <> 0
strRes.Append(Encoding.ASCII.GetString(Buffer, 0, iTotalBuff))
iTotalBuff = stream.Read(Buffer, 0, 128)
End While
strContents = strRes.ToString()
End Sub 'DownloadURL
End Class 'CSDownloadURL
End Namespace 'Project5

Use the CSDownloadURL

Class TestUrl
Public Shared Sub Main()
Dim strOut As String
im
web As New CSDownloadURL()
web.setURL1(http://www.mindcracker.com)
web.DownloadURL(strOut)
Console.WriteLine(strOut)
End Sub 'Main
End Class 'TestUrl

share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor