Network Programming--wininet__ Large data

Source: Internet
Author: User
Tags exception handling gopher win32 stock prices

The purpose of an Internet client program is to access information about a network data source (server) through Internet protocols such as HTTP, FTP, and so on. The client program can access the server to obtain like 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 (typically an intranet).
To develop Internet client programs. The MFC Class Library provides a dedicated WIN32 Internet extension interface, which is WinInet. MFC encapsulates WinInet in a standard, Easy-to-use collection of classes. When writing WinInet client programs, you can either call the WIN32 function directly or use the WinInet class library.
Win32 Internet extensions provide access to common Internet protocols, including HTTP, FTP, and gopher. Gopher has faded out. With the WinInet programming interface, developers can write high-level Internet client programs without having to understand the details of Winsock, TCP/IP, and specific Internet protocols. WinInet provides a unified set of functions for all kinds of protocols (HTTP, FTP, and Gopher), the Win32 API interface. Using these unified set of functions greatly simplifies the programming of protocols such as HTTP and FTP, which makes it easy to integrate the Internet into your own applications. The conversion of the underlying protocol (for example, from FTP to HTTP) can be done only if the source code is slightly modified.
There are two ways to use WinInet in Visual C + + engineering. One is to call the Win32 Internet function directly, and the other is to use the WinInet class library.
MFC's encapsulation of WinInet is implemented by providing three CStdioFile derived classes. These three derived classes are: CInternetFile, CHttpFile, and CGopherFile. Because the Gopher protocol has been used infrequently, this article will no longer discuss cgopherfile. For developers, whether you've ever used cstdiofile,wininet or not is a good understanding and easy to use. It makes it easy to access Internet data, making the processing of Internet and local data transparent, and where the data is stored is no longer important.

The MFC WinInet classes have the following advantages:

· Buffer input and output

· Type-safe handling of data

· The parameters of many functions are the default values

· Exception handling for normal internet errors

· Automatically clear open handles and connections

Using the API functions provided by WinInet, you can:

· The HTTP protocol is used to download HTML pages, which are designed to transfer HTML pages between the server and the client browser.

· Send FTP requests to upload or download files and get directory information for the server. Downloading files via anonymous login is a typical application of FTP.

· Other applications based on HTTP and FTP protocols.

The general process for using WinInet is:

Establish connection--> send request--> close connection

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

Realize

Method

Establish a connection

Create a CInternetSession object, which is a prerequisite for WinInet Internet client applications

Open a URL

Establish a connection, call CInternetSession::OpenURL
function that returns a read-only resource object

Reading URL Data

Open a URL and call CInternetSession::QueryOption

Query Internet Options settings

Establish a connection, call CInternetFile::Read

Set an Internet option

Establish a connection, call Cinternetsession::setoption

To set a function called with state information

Establish a connection, call CInternetSession::EnableStatusCallback
Overriding the CInternetSession::OnStatusCallback function

Close connection

Clear open connections with the CInternetSession object method

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

C + + Classes

CInternetSession (parent class CObject)
CInternetConnection (parent class CObject)
CFtpConnection
CGopherConnection
CHttpConnection
CInternetFile (parent class CStdioFile)
CGopherFile
CHttpFile
CFileFind (parent class CObject)
CFtpFileFind
CGopherFileFind
CGopherLocator (parent class CObject)
CInternetException (parent class CException)

Full function:

AfxParseURL
Afxgetinternethandletype
Afxthrowinternetexception

These classes and full functions except cfilefind in the AFX.H declaration, the rest are declared in the AFXINET.H file. They are highly abstract for protocols such as HTTP, FTP, and Gopher, and form a set of advanced API functions. Use these APIs to quickly and directly develop Internet applications. For example, connecting to an FTP server typically takes several steps and requires some low-level processing. However, using the APIs provided by the MFC class above, you can easily establish a connection by simply making one call to CInternetSession::GetFtpConnection.
As you know, every Internet application of its data exchange is based on the Internet Conversation (session), MFC is through the CInternetSession class object to implement the Internet session. Not only can you create a session with this class, but you can create several concurrent Internet sessions.
In order to communicate with the server, in addition to creating the CInternetSession object, you must create a CInternetConnection object that has three types of CInternetConnection objects for different protocols:

· CInternetSession::GetFtpConnection

· CInternetSession::GetHttpConnection

· CInternetSession::GetGopherConnection

These function calls do not read and write to files on the server. If you want to read and write data, you must open the file to operate. The processing process should be like this:

· First create the CInternetSession object instance

· If you create a session to read and write to a file, you must create an instance of the CInternetFile object (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 Uniform Resource Locator (URL) you provide, and then opens the server connection specified with the URL, returning a read-only CInternetFile object. CInternetSession::OpenURL is not intended for a specific protocol type-either FTP or HTTP-and it can even handle local files, at which point it returns CStdioFile instead of CInternetFile.

· If you create a session that does not read or write to a file, but to implement other tasks, such as deleting files in an FTP directory, you do not need to create a CInternetFile instance.

There are two ways to create a CInternetFile object:

· If you use CInternetSession::OpenURL to establish a connection to the server, the call returns CStdioFile.

· If you use CInternetSession::GetFtpConnection, getgopherconnection, or chttpconnection::openrequest to establish a connection to the server, You must call the corresponding Cftpconnection::openfile, cgopherconnection::openfile or CHttpConnection::OpenRequest, The returned content also corresponds to CInternetFile, CGopherFile, or CHttpFile.

To sum up, the steps to implement an Internet client application vary depending on the protocol. It depends on whether you are creating a general Internet client application based on OpenURL or an Internet client application for a particular protocol using one of the getxxxconnection functions.

Wininet API List of Functions

Platform sdk:windows Internet

WinINet functions
The following are the WinINet functions.

Commiturlcacheentry
Createmd5ssohash
Createurlcacheentry
Createurlcachegroup
Deleteurlcacheentry
Deleteurlcachegroup
Detectautoproxyurl
Findcloseurlcache
Findfirsturlcacheentry
Findfirsturlcacheentryex
FindFirstUrlCacheGroup
FindNextUrlCacheEntry
Findnexturlcacheentryex
Findnexturlcachegroup
FtpCommand
Ftpcreatedirectory
Ftpdeletefile
Ftpfindfirstfile
Ftpgetcurrentdirectory
FtpGetFile
Ftpgetfilesize
FtpOpenFile
FtpPutFile
Ftpremovedirectory
Ftprenamefile
Ftpsetcurrentdirectory
Geturlcacheentryinfo
Geturlcacheentryinfoex
Geturlcachegroupattribute
Gopherattributenumerator
Gophercreatelocator
Gopherfindfirstfile
Gophergetattribute
Gophergetlocatortype
Gopheropenfile
Httpaddrequestheaders
HttpEndRequest
HttpOpenRequest
HttpQueryInfo
HttpSendRequest
HttpSendRequestEx
Internetattemptconnect
Internetautodial
Internetautodialhangup
Internetcanonicalizeurl
Internetcheckconnection
InternetCloseHandle
Internetcombineurl
Internetconfirmzonecrossing
InternetConnect
Internetcrackurl
Internetcreateurl
Internetdeinitializeautoproxydll
InternetDial
InternetErrorDlg
Internetfindnextfile
InternetGetConnectedState
Internetgetconnectedstateex
InternetGetCookie
Internetgetcookieex
Internetgetlastresponseinfo
InternetGetProxyInfo
Internetgoonline
InternetHangUp
InternetInitializeAutoProxyDll
Internetlockrequestfile
InternetOpen
InternetOpenUrl
Internetquerydataavailable
InternetQueryOption
InternetReadFile
Internetreadfileex
InternetSetCookie
Internetsetcookieex
Internetsetdialstate
Internetsetfilepointer
InternetSetOption
Internetsetoptionex
Internetsetstatuscallback
Internetstatuscallback
Internettimefromsystemtime
Internettimetosystemtime
Internetunlockrequestfile
InternetWriteFile
Readurlcacheentrystream
Resumsuspendeddownload
Retrieveurlcacheentryfile
Retrieveurlcacheentrystream
Seturlcacheentrygroup
Seturlcacheentryinfo
Seturlcachegroupattribute
Unlockurlcacheentryfile
Unlockurlcacheentrystream

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.