Click here to Skip to main content
15,881,516 members
Articles / Desktop Programming / WTL
Article

INET For WTL

Rate me:
Please Sign up or sign in to vote.
4.86/5 (10 votes)
12 Aug 20051 min read 57K   984   29   7
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


Written By
Web Developer
Israel Israel
Gilad was born accidently to a pair of old lesbians. His childhood was full of vibrators and drugs. Married without kids. Has 14 grandsons around the world, 4 crocodiles, 2 mushrooms and a green alien living behind the refrigerator.

Hobbies: Watching hardcore porn, sculpturing with snot, skydiving from stairs.

Check my Homepage for additional resources.

Quote: "There's always one more bug"

Comments and Discussions

 
GeneralSmaller bug Pin
simonchen.net13-Feb-11 3:01
simonchen.net13-Feb-11 3:01 
GeneralAccessing REST web services Pin
kapil_moondra9-Mar-10 22:10
kapil_moondra9-Mar-10 22:10 
Generalmfc library Pin
monsieur_jj11-May-08 22:14
monsieur_jj11-May-08 22:14 
General2 small fixes Pin
Mike Melnikov8-Sep-05 21:13
Mike Melnikov8-Sep-05 21:13 
GeneralInteresting Bio Pin
NormDroid13-Aug-05 1:04
professionalNormDroid13-Aug-05 1:04 
Generalnew wheels Pin
gnk12-Aug-05 21:02
gnk12-Aug-05 21:02 
GeneralRe: new wheels Pin
Gilad Novik13-Aug-05 6:10
Gilad Novik13-Aug-05 6:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.