Code Examples
  Home arrow Code Examples arrow Page 2 - Download a Web Page using the WebClient
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Download a Web Page using the WebClient
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 62
    2006-09-13

    Table of Contents:
  • Download a Web Page using the WebClient
  • Creating a Windows project to use the WebClient Class
  • Know your options
  • Create a form using the WebClient Control

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Download a Web Page using the WebClient - Creating a Windows project to use the WebClient Class


    (Page 2 of 4 )

    Create a Windows Application project by accessing the New Project from the File menu item. This opens the New project window. Choose the Windows Application as shown and change the default WindowsApplication1 name to something different. In the present case it is called ExploreWebClient.

    Change the default Form1.vb to WebPage.vb. Using the WebClient class we will download a web page, in this case, a blog from the site http://hodentek.blogspot.com/. This is a blog that requires no authentication; you may use any other web page as well. To the WebPage.vb form add a button, a textbox, and a WebBrowser control. If the WebBrowser control is not already in the Toolbox, you may add the control by going to Toolbox--> Choose Toolbox items...

    and click the hyperlink to open the Choose ToolBox Items Window as shown.

    This adds the control to the Toolbox and appears immediately when you click OK to the above after placing a check mark against the item.

    With these controls added, your page should appear as shown in the next picture. The area above the button is the textbox which has been set in design to show multi-lines and to show both scroll bars. It does not show the horizontal scroll bar for reasons which are not clear. The clear area below the button is the resized WebBrowser control.

    To the click event of the button add the following code as shown:

    Imports System.Net
     
    Imports System.IO
     
    Public Class WebPage
        
    Dim wclient As New WebClient
     
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
     
            Dim sr As New StreamReader(wclient. _
            OpenRead("http://hodentek.blogspot.com/"))
            Dim s As String = sr.ReadToEnd
            TextBox1.Text = s
            WebBrowser1.Navigate("http://hodentek.blogspot.com/")
        End Sub
     
     
    End Class

    The Imports System.NET allows us to use the WebClient class and spawn a new instance by calling the New method. The newly created instance can call one of its methods, OpenRead() to read the contents of the URL.

    This gets directly read into the Stream by the StreamReader, and all that is read is sent to the textbox. Since you are using the StreamReader, you need the second imports statement, System.IO.

    The WebBrowser1 ActiveX control is straightforward to code. Given an URL, it displays the web page.

    More Code Examples Articles
    More By Jayaram Krishnaswamy


       · You want to look at a web page in a windows application - the web client is a good...
       · I need to add a new textbox so tht acc to the users needs the webpage can be shown...
       · I need to add a textbox so tht if the user enters the webaddress.. the required URL...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT