Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Networking » Download a Web Page using InternetOpenUrl API

Download a Web Page using InternetOpenUrl API

This article describes about downloading a Web Page using InternetOpenUrl API.Before talking about InternetOpenUrl, we need to look into InternetOpen API.

Author Rank :
Page Views : 40244
Downloads : 605
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
url.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

This article has two parts. First introduction to WinInet APIs required to use InternetOpenUrl and  second a sample code. Sample code is a dialog base VC++/MFC application which is attached as Url.zip.

Before talking about InternetOpenUrl, we need to look into InternetOpen API.

InternetOpen

This function is root of all WinInet functions, and must be called before any WinInet function. The InternetOpen function initializes WinInet environment and prepares to call other WinInet functions. Basically this functions starts a new internet session. The WinInet is based on hierarchy so next level function will require the handle returned by InternetOpen. Here is syntax of InternetOpen function

HINTERNET InternetOpen( IN LPCSTR lpszAgent, IN WORD dwAccessType, IN LPCSTR lpszProxyName, IN LPCSTR lpszProxyBypass, IN WORD dwFlags );

where, lpszAgent - String containing the name of the application. dwAccessType specifies how this internet session should attempt to access the Internet.

lpszProxyName and lpszProxyBypass parameters are valid only you use INTERNET_OPEN_TYPE_PROXY as second parameter.  lpszProxyName is the name of proxy server, and lpszProxyBypass is list of proxy servers. You can specify more than one proxy in lpszProxyName parameter. lpszProxyBypass parameter allows to specify an IP address to bypass a proxy server. The fourth parameter , dwFlags, allows to control the internet session behavior. Value of this parameter is either INTERNET_FLAG_OFFLINE, puts internet session in offline mode, or INTERNET_FLAG_ASYNC, which foreces that all operations should be asynchronous. Here is an example,

HINTERNET hInternet = InternetOpen( "TestApp", INTERNET_OPEN_TYPE_PROXY, "prxy.server.com", 158.55.255.251, INTERNET_FLAG_ASYNC );

We will see this in more details in our example later.

After initializing WinInet, there are two ways to work with Internet. Either URL related requirement, or protocol related requirement. InternetConnect function is followed by protocol related functions and InternetOpenUrl is related to URLs.

Here is when to choose what ?

InternetOpenUrl - Download a web page, downoad an image via HTTP, download a file via FTP, or download a file via Gopher.

InternetConnect - HTTP POST/GET headers to send and retrieve data, work with FTP such as create, rename, delete directories or upload/download  file, use gopher locators. 

InternetConnect

InternetConnect is responsible for starting new HTTP, FTP, or Gopher session after establishing a connection using InternetOpen. A single application can have multiple InternetConnect objects, depends on the requirement of the application.

HINTERNET InternetConnect(IN HINTERNET hInternetSession, IN LPCSTR lpszServerName,
IN INTERNET_PORT nServerPort, IN LPCSTR lpszUsername, IN LPCSTR lpszPassword,
IN DWORD dwService, IN DWORD dwFlags, IN DWORD dwContext );

Opens an FTP, Gopher, or HTTP session for a given site. 

InternetCloseHandle

BOOL InternetCloseHandle( IN HINTERNET hInet );

This function closes opened internet connection and terminates any pending operations on the handle and discards any outstanding data.  

Working with URLs

InternetOpenUrl is the main function required to work with URLs after InternetOpen. Other functions are helper functions.

InternetOpenUrl

HINTERNET InternetOpenUrl(IN HINTERNET hInternetSession, IN LPCSTR lpszUrl,
IN LPCSTR lpszHeaders, IN DWORD dwHeadersLength, IN DWORD dwFlags,
IN DWORD dwContext );

This is a general function that an application can use to retrieve data over any of the protocols that the Win32 Internet functions support. This function is particularly useful when the application does not need to access the particulars of a protocol, but only requires the data corresponding to a URL. The InternetOpenUrl function parses the URL string, establishes a connection to the server, and prepares to download the data identified by the URL. The application can then use InternetReadFile (for files) or InternetFindNextFile (for directories) to retrieve the URL data. It is not necessary to call InternetConnect before InternetOpenUrl

Sample: Using InternetOpenUrl to download a URL

Downloading a web page's contents is pretty simple using InternetOpenUrl. Here are few simple steps:

  • Create a dialog based application with two buttons and two edit boxes. Set content edit box's as multiline. 



  • Add two variables using Class wizard, m_strURL and m_strContents, for each edit boxes.

  • Include <wininet.h> in your stdafx.h and link to wininet.lib in your project settings. WinInet.lib is in your Lib directory of Visual Studio.

  • And Add a handler for Download button using Class Wizard and add this piece of code:

    void COpenUrlDlg::OnOk()
    {
    UpdateData(TRUE);
    if ( m_strURL.IsEmpty() )
    return;
    HINTERNET hINet, hFile;
    hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
    if ( !hINet )
    {
    AfxMessageBox("InternetOpen Failed");
    return;
    }
    hFile = InternetOpenUrl( hINet, m_strURL, NULL, 0, 0, 0 );
    if ( hFile )
    {
    CHAR buffer[1024];
    DWORD dwRead;
    while ( InternetReadFile( hFile, buffer, 1023, &dwRead ) )
    {
    if ( dwRead == 0 )
    break;
    buffer[dwRead] = 0;
    m_strURL += buffer;
    }
    InternetCloseHandle( hFile );
    }
    InternetCloseHandle( hINet );
    UpdateData(FALSE);
    }

  • Now run your program, type URL with http and hit Download. You will see source code of the URL you have entered.

  • Don't forget to include wininet.h #include <wininet.h> in your stdafx.h.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
how to download aweb page using threads? by Rowyda On June 9, 2006

Hello

        This article is good and very useful but i want to know how to download a   webpage and divide it into threads to accelerate downloading of a webpage.

Thank you

 

Reply | Email | Modify 
Urgent Please!! by salman On February 21, 2008
I have to download an image from a specified website. e.g http://www.cssnz.org/flower.jpg and I want to save this image data into a file. After that I want to look that image. Please tell How can i do that. As I am new to this VC++ coding. I shall be very thankful to you.
Reply | Email | Modify 
Re: Urgent Please!! by Luong On September 23, 2010
void downLoadImageFromURL()
{
    CString strURL = "http://www.sachbaovn.vn/cover/s_562_Tro_choi.jpg";
    HINTERNET hINet, hFile;
    hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
    if ( !hINet )
    {
        AfxMessageBox("InternetOpen Failed");
        return;
    }
    hFile = InternetOpenUrl( hINet, strURL, NULL, 0, 0, 0 );

    CFile cf;
    
    cf.Open("C:\\s_562_Tro_choi.jpg",CFile::modeCreate|CFile::modeWrite);
    
    if ( hFile )
    {
        CHAR buffer[1024];
        DWORD dwRead;
        while ( InternetReadFile( hFile, buffer, 1023, &dwRead ) )
        {
            if ( dwRead == 0 )
                break;
            cf.Write(buffer,dwRead);
            buffer[dwRead] = 0;
            strURL += buffer;
        }
        InternetCloseHandle( hFile );
    }
    InternetCloseHandle( hINet );
    cf.Close();

}

Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.