Guide to developing Internet client applications with wininet (1)

Source: Internet
Author: User
Tags gopher stock prices

Guide to developing Internet client applications with wininet (1)
I. Overview

An Internet client program is used to access information of network data sources (servers) through Internet protocols such as HTTP and FTP. The client can access the server to obtain information such as weather forecasts, stock prices, important news data, and even exchange information with the server. Internet client programs can access the server through an external network (Internet) or an internal network (usually Intranet.
To develop Internet client programs. The MFC class library provides a dedicated Win32 Internet extension interface, that is, wininet. MFC encapsulates wininet in a standard and easy-to-use class set. When writing a wininet client program, you can either directly call the Win32 function or use the wininet class library.
Win32 Internet extensions provide access to common Internet protocols, including HTTP, FTP, and gopher. Gopher has gradually faded out. With the help of wininet programming interfaces, developers do not have to understand the details of WinSock, TCP/IP and specific Internet protocols to write high-level Internet client programs. Wininet provides a unified function set for all the protocols (HTTP, FTP, and Gopher), that is, Win32 API interfaces. Using these unified function sets, You can greatly simplify programming for HTTP, FTP, and other protocols, so that you can easily integrate the Internet into your own applications. The conversion of the underlying protocol (for example, from ftp to HTTP) can be completed by slightly modifying the source code.
There are two ways to use wininet in the Visual C ++ project. One is to directly call the Win32 Internet function, and the other is to use the wininet class library.
MFC encapsulates wininet by providing three Derived classes of cstdiofile. The three Derived classes are cinternetfile, chttpfile, and cgopherfile. Since the Gopher protocol is rarely used, this article will not discuss cgopherfile. For developers, whether or not you have used cstdiofile before, wininet is easy to understand and use. It makes it easy to access Internet data, making the processing of Internet data and local data consistent and transparent, and the data storage location is no longer important.

The MFC wininet class has the following advantages:

  • Buffer Input and Output
  • Secure processing of data types
  • Parameters of many functions are default values.
  • Exception Handling for common Internet errors
  • Automatically clear open handles and connections

Using the API functions provided by wininet, you can:

  • Download HTML pages over HTTP. HTTP is used to transfer HTML pages between the server and the client browser.
  • Send an FTP request to upload or download files and obtain the directory information of the server. Downloading files through anonymous login is a typical FTP application.
  • Other HTTP and FTP-based applications.

The general process of using wininet is as follows:

Figure 1

The following table describes the general steps for implementing an Internet client program:
(Table 1)

Implementation Method
Create a connection Creates a cinternetsession object, which is a prerequisite for wininet Internet client applications.
Open a URL Create a connection and call cinternetsession: Openurl
Function, returns a read-only resource object
Read URL data Open a URL and call cinternetsession: queryoption
Query Internet option settings Create a connection and call cinternetfile: Read
Set an Internet option Create a connection and call cinternetsession: setoption
Set a function called with status information Create a connection and call cinternetsession: enablestatuscallback
Override cinternetsession: onstatuscallback Function
Close connection Clear the opened connection using the cinternetsession object Method

To create an Internet client program, MFC provides the following C ++ classes and full functions:

C ++ class

Cinternetsession (parent class cobject) cinternetconnection (parent class cobject) Commit (parent class cstdiofile) cgopherfilechttpfilecfilefind (parent class cobject) Commit (parent class cobject) cinternetexception)

Full function:

AfxParseURLAfxGetInternetHandleTypeAfxThrowInternetException    

Except for cfilefind declared in afx. H, these classes and full functions are declared in afxinet. h. They are highly abstracted over HTTP, FTP, and Gopher protocols and form a set of advanced API functions. You can use these APIs to quickly and directly develop Internet applications. For example, connecting to an FTP server requires several steps and some underlying processing. However, to use the API provided by the above MFC class, you only need to call cinternetsession: getftpconnection once to easily establish a connection.
As you know, data exchange between each Internet application is based on Internet sessions. MFC implements Internet sessions through cinternetsession objects. This class can not only create sessions, but also create several concurrent Internet sessions.
In order to communicate with the server, in addition to creating a cinternetsession object, you must also create a cinternetconnection object. For different protocols, the cinternetconnection object has three types:

  • Cinternetsession: getftpconnection
  • Cinternetsession: gethttpconnection
  • Cinternetsession: getgopherconnection

These function calls do not read or write files on the server. If you want to read and write data, you must open the file before you can operate. The process should be as follows:

  • First, create a cinternetsession object instance.
  • If the created session needs to read and write files, you must create a cinternetfile object instance (or its subclass chttpfile and cgopherfile object instance ). In fact, the easiest way to read data is to call the cinternetsession: Openurl function. This function parses the unified Resource Identifier (URL) you provide, opens a connection to the server specified by the URL, and returns a read-only cinternetfile object. Cinternetsession: Openurl is not applicable to a specific protocol type. It can be called either through FTP or HTTP. It can even process local files. In this case, cstdiofile instead of cinternetfile is returned.
  • If the created session does not read or write files, but implements other tasks, such as deleting files in an FTP directory, you do not need to create a cinternetfile instance.

There are two methods to create a cinternetfile object:

  • If cinternetsession: Openurl is used to establish a connection with the server, the cstdiofile is returned.
  • If you use cinternetsession: getftpconnection, getgopherconnection, or chttpconnection: openrequest to establish a connection with the server, you must call the cftpconnection: openfile, cgopherconnection: openfile or chttpconnection: openrequest, the returned content also corresponds to cinternetfile, cgopherfile, or chttpfile.

To sum up, the steps for implementing an Internet client application vary by protocol. It depends on whether you create an Internet client application based on Openurl or use one of the getxxxconnection functions for Internet client applications with specific protocols.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.