Guide to developing Internet client applications with wininet (2)

Source: Internet
Author: User
Tags ftp connection ftp file ftp client
Guide to developing Internet client applications with wininet (2)
II. Implementation steps

As you know, every Internet client program is accompanied by certain behaviors, such as reading, writing, and deleting files. The prerequisite for a client program to implement these behaviors is to establish an Internet connection. Then perform specific operations for different purposes. For convenience, the following table lists the specific operations required for different application behaviors. It lists the methods required to achieve a certain goal for general Internet URL (FTP, or HTTP) Client behavior. 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 use "wininet" as the keyword to search for some technical information using MFC 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.