Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

INET For WTL

0.00/5 (No votes)
12 Aug 2005 3  
Internet classes for WTL developers.

Introduction

In this article, I'll introduce some wrapper classes I've wrote to use INET under WTL. To make things simple, the code uses C++ exceptions, so there is no need to check the result code of every function call.

Usage

All handles inherit from CInternetHandle, so there is no need to destroy them manually.

To start using INET, you first need to create a session. This can be done by creating a CInternetSession object. You can specify the user-agent in the constructor, or leave it blank (in that case, the code will use your application name as the user-agent string).

CInternetSession Session(_T("Test Application"));

Connections

Currently there are two types of connections: HTTP and FTP. To create a connection, you simply construct an object with the server parameters.

CHttpConnection Connection1(Session,_T("http://www.codeproject.com/"));
CFtpConnection Connection2(Session,_T("ftp://ftp.codeproject.com/"));

Files

All internet files inherit from CInternetFile. In addition, there are three special files: HTTP/HTTPS and FTP. You can create a file object via an already made connection, or create a new one via a session object.

BYTE Buffer[4096];

CHttpFile File1(Connection1,_T("GET"),_T("/"));
File1.SendRequest();
for (DWORD dwRead1;dwRead1=File1.Read(Buffer,sizeof(Buffer)););

CHttpFile File2(Session,_T("http://www.codeproject.com/"));
for (DWORD dwRead2;dwRead2=File2.Read(Buffer,sizeof(Buffer)););

When reading from files, there is also a special option, allowing you to time the download, as well as limit it to a certain download speed. You can do it by using the CInternetFile::CInfo class.

BYTE Buffer[4096];
CHttpFile File3(Session,_T("http://www.codeproject.com/"));
CInternetFile::CInfo Info(File3);
Info.SetRateLimit(10.5);
for (DWORD dwRead3;dwRead3=File3.Read(Buffer,sizeof(Buffer),Info);)
{
   // Info.GetTimeLeft() = Seconds left for download

}

Exceptions

When an error occurs, the code throws an exception (CInternetException). You can get the error message with the GetErrorMessage function.

try
{
  CInternetSession Session;
  CHttpFile File(Session,_T("http://www.codeproject.com/"));
}
catch(CInternetException& err)
{
  MessageBox(err.GetErrorMessage());
}

Bugs & Limitations

I've tried to implement a callback mechanism, but it still has some problems. Avoid using it for now. In addition, since I haven't had any real need for gopher, I never put it inside my code. You are welcome to add it if you need it. I'm using that code in my projects, but it may still contain some bugs. If you find any problem, please notify me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here