Click here to Skip to main content
Click here to Skip to main content

INET For WTL

, 12 Aug 2005
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Gilad Novik
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

 
GeneralInteresting Bio Pinmembernorm.net13-Aug-05 1:04 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 12 Aug 2005
Article Copyright 2005 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid