Link to home
Start Free TrialLog in
Avatar of etgarim
etgarim

asked on

How do I download an Internet file without MFC?

Hello,

I want to write a C++ code using Win32 and no MFC
to download and read an HTML file on the net.

Can you show me?

Thanks,
Amir Shaked
Avatar of Kimpan
Kimpan
Flag of Sweden image

You could use InternetOpenUrl in wininet API

InternetOpen Function

--------------------------------------------------------------------------------

Initializes an application's use of the Microsoft. Win32. Internet functions.

Syntax

HINTERNET InternetOpen(
    LPCTSTR lpszAgent,
    DWORD dwAccessType,
    LPCTSTR lpszProxyName,
    LPCTSTR lpszProxyBypass,
    DWORD dwFlags
);

Parameters

lpszAgent
[in] Pointer to a string variable that contains the name of the application or entity calling the Internet functions (for example, Microsoft. Internet Explorer). This name is used as the user agent in the HTTP protocol.
dwAccessType
[in] Type of access required. This can be one of the following values:
INTERNET_OPEN_TYPE_DIRECT
Resolves all host names locally.
INTERNET_OPEN_TYPE_PRECONFIG
Retrieves the proxy or direct configuration from the registry.
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY
Retrieves the proxy or direct configuration from the registry and prevents the use of a startup Microsoft. JScript. or Internet Setup (INS) file.
INTERNET_OPEN_TYPE_PROXY
Passes requests to the proxy unless a proxy bypass list is supplied and the name to be resolved bypasses the proxy. In this case, the function uses INTERNET_OPEN_TYPE_DIRECT.
lpszProxyName
[in] Pointer to a string variable that contains the name of the proxy server(s) to use when proxy access is specified by setting dwAccessType to INTERNET_OPEN_TYPE_PROXY. Do not use an empty string, because InternetOpen will use it as the proxy name. The Win32 Internet functions recognize only CERN type proxies (HTTP only) and the TIS FTP gateway (FTP only). If Internet Explorer is installed, the Win32 Internet functions also support SOCKS proxies. FTP and Gopher requests can be made through a CERN type proxy either by changing them to an HTTP request or by using InternetOpenUrl. If dwAccessType is not set to INTERNET_OPEN_TYPE_PROXY, this parameter is ignored and should be set to NULL. For more information about listing proxy servers, see the Listing Proxy Servers section of Enabling Internet Functionality.
lpszProxyBypass
[in] Pointer to a string variable that contains an optional list of host names or IP addresses, or both, that should not be routed through the proxy when dwAccessType is set to INTERNET_OPEN_TYPE_PROXY. The list can contain wildcards. Do not use an empty string, because InternetOpen will use it as the proxy bypass list. If this parameter specifies the "<local>" macro as the only entry, the function bypasses any host name that does not contain a period. If dwAccessType is not set to INTERNET_OPEN_TYPE_PROXY, this parameter is ignored and should be set to NULL.
dwFlags
[in] Unsigned long integer value that contains the flags that indicate various options affecting the behavior of the function. This can be a combination of these values:
INTERNET_FLAG_ASYNC
Makes only asynchronous requests on handles descended from the handle returned from this function.
INTERNET_FLAG_FROM_CACHE
Does not make network requests. All entities are returned from the cache. If the requested item is not in the cache, a suitable error, such as ERROR_FILE_NOT_FOUND, is returned.
INTERNET_FLAG_OFFLINE
Identical to INTERNET_FLAG_FROM_CACHE. Does not make network requests. All entities are returned from the cache. If the requested item is not in the cache, a suitable error, such as ERROR_FILE_NOT_FOUND, is returned.
Return Value

Returns a valid handle that the application passes to subsequent Win32 Internet functions. If InternetOpen fails, it returns NULL. To retrieve a specific error message, call GetLastError .
Remarks

InternetOpen is the first Win32 Internet function called by an application. It tells the Internet DLL to initialize internal data structures and prepare for future calls from the application. When the application finishes using the Internet functions, it should call InternetCloseHandle to free the handle and any associated resources.

The application can make any number of calls to InternetOpen, though a single call is normally sufficient. The application might need to define separate behaviors for each InternetOpen instance, such as different proxy servers configured for each.

After the calling application has finished using the HINTERNET handle returned by InternetOpen, it must be closed using the InternetCloseHandle function.

Function Information

Stock Implementation wininet.dll
Custom Implementation No
Header Wininet.h
Import library Wininet.lib
Minimum availability Internet Explorer 3.0
Minimum operating systems Windows NT 4.0, Windows 95, Windows CE 2.12

See Also

Introduction to the Microsoft Win32 Internet Functions, Enabling Internet Functionality, WinInet API
sorry, wrong function

InternetOpenUrl Function

--------------------------------------------------------------------------------

Opens a resource specified by a complete FTP, Gopher, or HTTP URL.

Syntax

HINTERNET InternetOpenUrl(
    HINTERNET hInternet,
    LPCTSTR lpszUrl,
    LPCTSTR lpszHeaders,
    DWORD dwHeadersLength,
    DWORD dwFlags,
    DWORD_PTR dwContext
);

Parameters

hInternet
[in] HINTERNET handle to the current Internet session. The handle must have been returned by a previous call to InternetOpen.
lpszUrl
[in] Pointer to a string variable that contains the URL to begin reading. Only URLs beginning with ftp:, gopher:, http:, or https: are supported.
lpszHeaders
[in] Pointer to a string variable that contains the headers to be sent to the HTTP server. (For more information, see the description of the lpszHeaders parameter in the HttpSendRequest function.)
dwHeadersLength
[in] Unsigned long integer value that contains the length, in TCHARs, of the additional headers. If this parameter is -1L and lpszHeaders is not NULL, lpszHeaders is assumed to be zero-terminated (ASCIIZ) and the length is calculated.
dwFlags
[in] Unsigned long integer value that contains the API flags. This can be one of the following values:
INTERNET_FLAG_EXISTING_CONNECT
Attempts to use an existing InternetConnect object if one exists with the same attributes required to make the request. This is useful only with FTP operations, since FTP is the only protocol that typically performs multiple operations during the same session. The Microsoft. Win32. Internet API caches a single connection handle for each HINTERNET handle generated by InternetOpen.
INTERNET_FLAG_HYPERLINK
Forces a reload if there was no Expires time and no LastModified time returned from the server when determining whether to reload the item from the network.
INTERNET_FLAG_IGNORE_CERT_CN_INVALID
Disables Win32 Internet function checking of SSL/PCT-based certificates that are returned from the server against the host name given in the request. Win32 Internet functions use a simple check against certificates by comparing for matching host names and simple wildcarding rules.
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
Disables Win32 Internet function checking of SSL/PCT-based certificates for proper validity dates.
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP
Disables the ability of the Win32 Internet functions to detect this special type of redirect. When this flag is used, Win32 Internet functions transparently allow redirects from HTTPS to HTTP URLs.
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS
Disables the ability of the Win32 Internet functions to detect this special type of redirect. When this flag is used, Win32 Internet functions transparently allow redirects from HTTP to HTTPS URLs.
INTERNET_FLAG_KEEP_CONNECTION
Uses keep-alive semantics, if available, for the connection. This flag is required for Microsoft Network (MSN), NT LAN Manager (NTLM), and other types of authentication.
INTERNET_FLAG_NEED_FILE
Causes a temporary file to be created if the file cannot be cached.
INTERNET_FLAG_NO_AUTH
Does not attempt authentication automatically.
INTERNET_FLAG_NO_AUTO_REDIRECT
Does not automatically handle redirection in HttpSendRequest.
INTERNET_FLAG_NO_CACHE_WRITE
Does not add the returned entity to the cache.
INTERNET_FLAG_NO_COOKIES
Does not automatically add cookie headers to requests, and does not automatically add returned cookies to the cookie database.
INTERNET_FLAG_NO_UI
Disables the cookie dialog box.
INTERNET_FLAG_PASSIVE
Uses passive FTP semantics. InternetOpenUrl uses this flag for FTP files and directories.
INTERNET_FLAG_PRAGMA_NOCACHE
Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy.
INTERNET_FLAG_RAW_DATA
Returns the data as a GOPHER_FIND_DATA structure when retrieving Gopher directory information, or as a WIN32_FIND_DATA  structure when retrieving FTP directory information. If this flag is not specified or if the call was made through a CERN proxy, InternetOpenUrl returns the HTML version of the directory.
INTERNET_FLAG_RELOAD
Forces a download of the requested file, object, or directory listing from the origin server, not from the cache.
INTERNET_FLAG_RESYNCHRONIZE
Reloads HTTP resources if the resource has been modified since the last time it was downloaded. All FTP and Gopher resources are reloaded.
INTERNET_FLAG_SECURE
Uses secure transaction semantics. This translates to using Secure Sockets Layer/Private Communications Technology (SSL/PCT) and is only meaningful in HTTP requests.
dwContext
[in] Pointer to an unsigned long integer value that contains the application-defined value that is passed, along with the returned handle, to any callback functions.
Return Value

Returns a valid handle to the FTP, Gopher, or HTTP URL if the connection is successfully established, or NULL if the connection fails. To retrieve a specific error message, call GetLastError . To determine why access to the service was denied, call InternetGetLastResponseInfo.
Remarks

Call InternetCanonicalizeUrl first if the URL being used contains a relative URL and a base URL separated by blank spaces.

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 especially 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.

InternetOpenUrl disables Gopher on ports less than 1024, except for port 70the standard Gopher portand port 105typically used for Central Services Organization (CSO) name searches.

After the calling application has finished using the HINTERNET handle returned by InternetOpenUrl, it must be closed using the InternetCloseHandle function.

Function Information

Stock Implementation wininet.dll
Custom Implementation No
Header Wininet.h
Import library Wininet.lib
Minimum availability Internet Explorer 3.0
Minimum operating systems Windows NT 4.0, Windows 95, Windows CE 2.12

See Also

Introduction to the Microsoft Win32 Internet Functions, Handling Uniform Resource Locators, WinInet API
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as mentioned by other experts you need include Wininet.h and link with wininet.lib for this to work ..

hope it helped ..
Avatar of alan93
alan93

I got an error with the accepted answer from ambience.

error C2660: 'HttpQueryInfoA' : function does not take 4 parameters
DWORD dwIndex = 0;
char bufQuery[32] ;
DWORD dwLengthBufQuery = sizeof(bufQuery);
BOOL bQuery = ::HttpQueryInfo(hHttpFile,
                             HTTP_QUERY_CONTENT_LENGTH,
                             bufQuery,
                             &dwLengthBufQuery,
                             &dwIndex) ;