|
|
|
|
|
|
Author Rank:
|
|
Total page views :
35409
|
|
Total downloads :
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is a software developer with over 13 years of 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.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from
DevExpress - Absolutely Free-of-Charge without any royalties or distribution
costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin
edit, tab control and so much more!
DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
|
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.
|
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
|
|
|
|
|
|
|
|
|
|
|
|
|