|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
41092
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
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.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|