看板 ott
作者 標題 WinInet MFC Classes
時間 2010年01月13日 Wed. PM 04:08:22
WinInet MFC Classes
Here is MFC classes' hierarchy. In MFC hierarchy you see
CFtpConnection, CHttpConnection, and CGopherConnection classes are
derived from CInternetConnection.
CInternetSession
CInternetSession is the first class to start an Internet session.
This class warps InternetOpen API. Constructor of this class calls
InternetOpen WinInet API, which opens an Internet connection.
Other important function of this class is OpenUrl. This function
actually calls InternetOpenUrl to open a URL. Here is the example:
CInternetSession session;
CStdioFile* file = session.OpenURL(
"http://www.dotnetheaven.com");
You can establish a connection with HTTP, FTP, or Gopher servers
by calling its GetHttpConnection,
GetFtpConnection or GetGopherConnection functions respectively.
You can manage cookies by using three
functions SetCookie, GetCookie, and GetCookieLength.
Lets take a quick look on other CInternetSession functions. You
can see MSDN for more details.
Function
Description
QueryOpen
Provides possible five asserts for error checking.
SetOption
This member function is used to set options for
the Internet session. See InternetQueryOption's dwOption parameter
for more details.
OpenURL
This function is a wrapper for InternetOpenURL
API, which allows only downloading and reading the data from a
URL.
GetFtpConnection
Call this member function to establish an
FTP connection and get a pointer to a CFtpConnection object.
GetHttpConnection
Opens an HTTP server for an application
that is trying to open a connection. You calll this member
function to establish an HTTP connection and get a pointer to a
CHttpConnection object.
GetGopherConnection
Call this member function to establish a
new gopher connection and get a pointer to a CGopherConnection
object.
EnableStatusCallback
Establishes a status callback routine.
EnableStatusCallback is required for asynchronous operations.
ServiceTypeFromHandle
Gets the type of service from the Internet
handle.
GetContext
Gets the context value for an Internet or
application session.
Close
Closes the Internet connection when the Internet session
is terminated.
SetCookie
Sets a cookie for the specified URL.
GetCookie
Returns cookies for the specified URL and all its
parent URLs.
GetCookieLength
Retrieves the variable specifying the length of
the cookie stored in the buffer.
OnStatusCallback
Updates the status of an operation when
status callback is enabled.
CInternetFile
As you can see in the hierarchy chart, CInternetFile class is
derived from CStdioFile. CInternetFile is base class
for CHttpFile and CGopherFile, which allows you to access remote,
files on HTTP and Gopher servers. Here are
its members.
Members
Description
SetWriteBufferSize
Sets the size of the buffer where data
will be written.
SetReadBufferSize
Sets the size of the buffer where data
will be read.
Seek
Repositions the pointer in an open file.
Read
Read the # of specified bytes.
Write
Write the number of specified bytes.
Abort
Closes the file, ignoring all warnings and errors.
Flush
Flushes the contents of the write buffer and makes sure
the data in memory is written to the target machine.
Close
Closes a CInternetFile and frees its resources
ReadString
Reads a stream of characters.
m_hFile
A handle to a file.
Here is how to use this class:
HINTERNET hRequest = HttpOpenRequest( hConnection, "GET", "",
NULL, NULL, INTERNET_FLAG_RELOAD, 0 );
NOTE: You must call delete to free this file from memory.
CInternetConnection
This class is a base class for CHttpConnection, CFtpConnection,
and CGopherConnection classes. You always
create this class using methods of CInternetSession. Since you
create this class dynamically using
CInternetSession, you must always call delete to free it from
memory. You probably don't have to use in your
program. Its has only three member functions.
Members
Description
GetContext
Gets the context ID for this connection object.
GetSession
Gets a pointer to the CInternetSession object
associated with the connection.
GetServerName
Gets the name of the server associated with the
connection.
WinInet HTTP MFC Classes
CHttpConnection
This class manages connection to an HTTP server. You create this
class object dynamically by calling
CInternetSession::GetHttpConnection. You never create
CHttpConnection directly. This class has only one
member fdunction:
Members
Description
OpenRequest
Opens an HTTP request.
This function returns a pointer to CHttpFile. Here are syntaxes of
this function:
CHttpFile* OpenRequest( LPCTSTR pstrVerb, LPCTSTR pstrObjectName,
LPCTSTR pstrReferer = NULL, DWORD
dwContext = 1, LPCTSTR* pstrAcceptTypes = NULL, LPCTSTR
pstrVersion = NULL, DWORD dwFlags =
INTERNET_FLAG_EXISTING_CONNECT );
CHttpFile* OpenRequest( int nVerb, LPCTSTR pstrObjectName, LPCTSTR
pstrReferer = NULL, DWORD dwContext
= 1, LPCTSTR* pstrAcceptTypes = NULL, LPCTSTR pstrVersion = NULL,
DWORD dwFlags =
INTERNET_FLAG_EXISTING_CONNECT );
Here I am interested in nVerb parameter only. See MSDN for more
details.
nVerb This parameter tells the request type to the server number
It Can be one of the following:
HTTP request type
nVerb value
HTTP_VERB_POST
0
HTTP_VERB_GET
1
HTTP_VERB_HEAD
2
HTTP_VERB_PUT
3
HTTP_VERB_LINK
4
HTTP_VERB_DELETE
5
HTTP_VERB_UNLINK
6
Here is how to use this class:
HINTERNET hRequest = HttpOpenRequest( hConnection, "GET", "",
NULL, NULL, INTERNET_FLAG_RELOAD, 0 );
CHttpFile
This class encapsulates rest of the HTTP APIs. All methods are
easy to understand.
Members
Description
AddRequestHeaders
Adds headers to the request sent to an
HTTP server.
SendRequest
Sends a request to an HTTP server.
SendRequestEx
Sends a request to an HTTP server using the Write
or WriteString methods of CInternetFile.
EndRequest
Ends a request sent to an HTTP server with the
SendRequestEx member function.
QueryInfo
Returns the response or request headers from the
HTTP server.
QueryInforStatusCode
Retrieves the status code associated with
an HTTP request and places it in the supplied dwStatusCode
parameter.
GetVerb
Gets the verb that was used in a request to an HTTP
server.
GetObject
Gets the target object of the verb in a request to
an HTTP server.
GetFileURL
Gets the URL for the specified file.
Close
Closes the CHttpFile and frees its resources.
WinInet FTP MFC Classes
FTP Classes
CFtpConnection and CFtpFindFile are two FTP classes, which
encapsulate all MFC APIs.
CFtpConnection
This class manages connection to your FTP server and allows direct
manipulation of files and directories on the
server. You always create a CFtpConnection object using
CInternetSession::GetFtpConnection. All member of
this class are easy to understand. We will see how to use these
functions in our sample example.
Members
Description
SetCurrentDirectory
Sets the current FTP directory.
GetCurrentDirectory
Gets the current FTP directory.
GetCurrentDirectoryAsURL
Get current dir as a URL.
RemoveDirectory
Removes the specified dir.
CreateDirectory
Creates a new dir.
Rename
Rename a file or a dir.
Remove
Removes a file.
PutFile
Uploads a file to the server.
GetFile
Download a file from the server.
OpenFile
Opens a file and returns a pointer to
CInternetFile.
Close
Closes the connection.
CFtpFindFile
CFindFileFile is used to find a file on the server.
Members
Description
FindFile
Finds a file on the server. It returns nonzero if
successful, otherwise 0.
FindNextFile
Use to continue to search for a file, which you
started with FindFile.
GetFileURL
Gets the URL for a specified file.
Some code snippets:
CFtpConnection* m_pFtpConnection;
m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName,
szftpUserID, szftpPassword, nPort,
FALSE);
// Do connection to the FTP server
if ( m_pFtpConnection->PutFile(lpExePath + "default.asp",
"default.asp", FTP_TRANSFER_TYPE_BINARY, 1) ==
1 )
AddLog("File : default.asp uploaded successfully.");
else
AddLog("Error in file :default.asp upload.");
FTP Example: MSDN has an excellent example of FTP. There is
nothing left after that example.
WinInet Gopher MFC Classes
Gopher Classes
CGopherConnection, CGopherFile, CGopherFindFile, and
CGopherLocator are four gopher classes. I don't see any
practical use of Gopher here. So why don't I save some of my time
of next tutorial. May be some of you guys
will complete this part :). Let's party tonite.
Other Classes
CInternetExeption
This class is for internet exception handling. It is derived from
CException. You will see use of this class in my
examples.
Other chapters of this tutorial
Introduction to WinInet.
Working with Common WinInet APIs.
WinInet APIs for HTTP and FTP with sample examples.
Advanced WinInet and Security Issues.
http://www.dotnetheaven.com/uploadfi...85341am/wininetmfcclasses.aspx
--
※ 來源: DISP BBS (http://disp.twbbs.org)
※ 作者: ott 來自: 118.166.4.170 時間: 2010-01-13 16:08:22
※ 看板: ott 文章推薦值: 0 目前人氣: 0 累積人氣: 267
回列表(←)
分享