Monday, August 23, 2010

Working with common WinInet APIs

This article has two parts. First part is the introduction to WinInet APIs required using InternetOpenUrl and second part is sample code. Sample code is a dialog base VC++/MFC application that 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 function 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 controlling 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 b 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.

Read more: C# Corner

Posted via email from .NET Info