Guide to developing Internet client applications with wininet

Source: Internet
Author: User
Tags ftp connection ftp file gopher ftp client stock prices

Guide to developing Internet client applications with wininet (1)

Compile/northtibet

I. Overview

An Internet client program is used to access information of network data sources (servers) through Internet protocols such as HTTP and FTP. Client programs can access the server
Like weather forecasts, stock prices, important news data, and even information exchange with servers. The Internet client program can use an external network (Internet) or an internal network (generally
Intranet.
To develop Internet client programs. The MFC class library provides a dedicated Win32 Internet
Extended Interface, that is, wininet. MFC encapsulates wininet in a standard and easy-to-use class set. When writing a wininet client program, you can directly call
Win32 function, you can also use the wininet class library.
Win32 Internet
Extensions provide access to common Internet protocols, including HTTP, FTP, and gopher. Gopher has gradually faded out. Programming with wininet
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 several protocols (HTTP, FTP, and Gopher), that is, Win32.

API. 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. Conversion of underlying protocols
(For example, from ftp to HTTP) you only need to slightly modify the source code.
There are two ways to use wininet in the Visual C ++ project. One is to directly call Win32
Internet functions. The other is to use the wininet class library.
MFC encapsulates wininet by providing three Derived classes of cstdiofile. These 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 and wininet are both easy to understand and use. It makes it easy to access Internet data, making Internet data and local data processing
Consistent and transparent data storage locations are 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) cftpconnection cgopherconnection timeout (parent class cstdiofile) Publish timeout (parent class cobject) cftpfilefind timeout (parent class cobject) Timeout (parent class cexception)

Full function:

AfxParseURLAfxGetInternetHandleTypeAfxThrowInternetException    

Except for cfilefind declared in afx. H, these classes and full functions are declared in afxinet. h. They perform HTTP, FTP, Gopher, and other 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, the above MFC class provides
You only need to call cinternetsession: getftpconnection once to establish a connection easily.

As you know, the data exchange between each Internet application is based on the internet session.
Cinternetsession class object to implement Internet sessions. 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, 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 specific protocol types. It can be called either over FTP or HTTP. It can even process local files.
    It is cstdiofile, not cinternetfile.

  • 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 cinternetsession: getftpconnection, getgopherconnection
    Or chttpconnection: openrequest to establish a connection with the server, you must call the corresponding
    Cftpconnection: openfile, cgopherconnection: openfile or
    Chttpconnection: openrequest. The returned content is also consistent with 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.

In subsequent articles, we will further discuss the steps and details of using wininet to implement Internet client applications.

II. Implementation steps

As you know, every Internet client program is accompanied by certain behaviors, such as reading, writing, and deleting files. A client program must establish
Internet connection. Then perform specific operations for different purposes. For convenience, the following table lists the specific operations required for different application behaviors. General
Internet
URL (FTP, or
HTTP) the method required for client behavior to achieve a certain goal. The content of this table is from msdn. I have added some of what I think is important.

(Table 1) process of a typical Internet client program

Purpose Method Result
Start an internet session Create a cinternetsession object Initialize wininet and connect to the server
Read or set the internetquery option (for example, timeout or number of retries) Call cinternetsession: setoption If the call fails, false is returned.
Establish a callback function to monitor the session Status Call cinternetsession: enablestatuscallback
Create callback function
Cinternetsession: onstatuscallback, override onstatuscallback, and create your own callback routine
Internet server Intranet server or local file Call cinternetsession: Openurl Parse and open the connection to the specified server, and return the cstdiofile (if the Openurl you passed is a local file name) or cinternetfile object. By accessing this object, you can obtain the server or file data.
Read files Call cinternetfile: Read Use the buffer you provided to read the specified number of bytes.
Exception Handling Processing in the cinternetexception class Handle all common Internet exception types
End internet session Processing Cinternetsession object Automatically clear the connection to the opened handle


(Table 2) typical FTP client implementation steps

Purpose Method Result
Start an FTP session and establish an FTP connection Create a cinternetsession object and call cinternetsession: getftpconnection Initialize wininet
Connect to the server
Connect to an FTP server Use cinternetsession: getftpconnection Returns a cftpconnection object.
CD to a new directory on the FTP server Use cftpconnection: setcurrentdirectory CD to an FTP server
New directory
Find Files in the first FTP directory Create a cftpfilefind object, call cftpfilefind: findfile, The Openurl function returns a read-only resource object, and call cftpfilefind: findfile Find the first file. If the file is found, false is returned.
Enumerate all available resources and find the files in the next FTP directory Find the next resource and call cftpfilefind: findnextfile until false is returned. Find Next file
If the file is not found, false is returned.
Open the file found in findfile or findnextfile (for read/write) Call cftpconnection: openfile. The parameter is the file name returned by findfile or findnextfile.
, Create and open a cinternetfile object
Open the file (for read and write) found in findfile or findnextfile, and return a cinternetfile object.
Read/write files Open the FTP file in Read mode and use cinternetfile: Read Use the specified buffer read
Specified number of bytes
Write FTP files Open the FTP file in write mode, call cinternetfile: write, and rewrite cinternetsession: onstatuscallback. Use your specified buffer to write
Specified number of bytes
Change the client directory on the server Call cftpconnection: setcurrentdirectory Enter a new directory
Obtains the current directory of the client on the server. Call cftpconnection: getcurrentdirectory Get directory information
Exception Handling Use cinternetexception class Handle all common Internet exception types
End FTP session Process cinternetsession objects Automatically clear the connection to the opened handle


(Table 3) shows the general steps to implement a typical FTP client application for deleting files:

Purpose Method Result
Start an FTP session Create a cinternetsession object Initialize wininet
Connect to the server
Connect to an FTP server Use cinternetsession: getftpconnection Returns a cftpconnection object.
Check whether the FTP directory is correct Use cftpconnection: getcurrentdirectory or cftpconnection: getcurrentdirectoryasurl Returned directory name
The URL of the server directory or returned directory
CD (Change directory) to a new directory on the FTP server Use cftpconnection: setcurrentdirectory CD to an FTP server
New directory
Find Files in the first FTP directory Use cftpfilefind: findfile Find the first file. If the file is found, false is returned.
Find Files in the next FTP directory Use cftpfilefind: findnextfile Find Next file
If the file is not found, false is returned.
Delete the file found in findfile or findnextfile Use cftpconnection: Remove to use the file name returned by findfile or findnextfile Delete findfile or findnextfile
File Found
Exception Handling Use cinternetexception class Handle all common Internet exception types
End FTP session Process cinternetsession objects Automatically clear the connection to the opened handle

(Table 4) shows the general steps for implementing a typical HTTP client application:

Purpose Method Result
Start an http session and establish an HTTP Connection Create a cinternetsession object and call cinternetsession: gethttpconnection
Create a chttpconnection object
Initialize wininet and connect to the server. A chttpconnection object is returned.
Create an HTTP request Call chttpconnection: openrequest
Create a chttpfile object;
Returns a chttpfile object.
Send an HTTP request Use chttpfile: addrequestheaders and use chttpfile: sendrequest Find a file
If the file is not found, false is returned.
Read files Call cinternetfile: Read Use the buffer you provided to read the specified byte
Get http Request Information Call chttpfile: queryinfo Obtain HTTP request header information from the server
Exception Handling Use the cinternetexception class Handle all common Internet exception types
End http session Process cinternetsession objects Automatically clear the connection to the opened handle

Due to the time relationship, I did not write the sample code in this article. However, there are two simple examples in msdn for reference: ftptree and tear. In addition, you can also use "wininet" as the keyword to search for it in Google and find some methods to use MFC.
Technical information of wininet. (End)

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.