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

INET For WTL

By , 12 Aug 2005
 

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
Member
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"

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralSmaller bugmemberstarschen13 Feb '11 - 3:01 
I found a smaller bug with CFtpFile class , see the original code below:
CFtpFile(HINSTANCE hConnection,LPCTSTR szFilename,DWORD dwAccess,DWORD dwFlags=FTP_TRANSFER_TYPE_UNKNOWN) throw(...)
 
HINSTANCE
should be replaced by
HINTERNET
 
Anyway, those classes saved lots of time for me..
 
Thank you. Smile | :)
Chen
GeneralAccessing REST web servicesmemberkapil_moondra9 Mar '10 - 22:10 
Hi,
 
I've been accessing REST Web services (developed in C#) thru this code and it was working fine.
 
But now there is a requirement, as per which, we have to put an authentication on accessing the web services (In Transport Layer).
 
Now i'm setting the INTERNET_OPTION_USERNAME and INTERNET_OPTION_PASSWORD option thru SetOption, but Its not working. Its simply not returning anything. No Error, No File at all.
 
What can be the reason for this.
 
GetResultFromWebService(CString strPrefix, CString strURL, int &nError)
{
 
CInternetSession *pSession = NULL;
try
{
	pSession = new CInternetSession(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 
			INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION);
 
        CHttpFile sourceFile(*pSession, strURL, 0, (DWORD)-1, INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION);	
	sourceFile.SetOption(INTERNET_OPTION_USERNAME, _T("xyz"));
	sourceFile.SetOption(INTERNET_OPTION_PASSWORD, _T("abc"));
        CAtlFile destFile;
	strTempPath =  CHelperClass::SubsXMLTempPath(strPrefix);
	HRESULT		hr = destFile.Create(strTempPath, GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS);
	BYTE buffer[4096];
	DWORD dwRead;
 
	// Read in 4096-byte blocks,
	// remember how many bytes were actually read,
	// and try to write that many out. This loop ends
	// when there are no more bytes to read.
	int nCount = 0;
	do
	{
		dwRead = sourceFile.Read(buffer, 4096);
		if(dwRead==0 && nCount == 0)
		{
			nError = NO_RESULT_FROM_WEBSERVICE ;
                        break;
		}
		destFile.Write(buffer, dwRead);
		nCount++;
	}
	while (dwRead > 0);
	sourceFile.Close();
	destFile.Close();
	pSession->Close();
} 
}

Generalmfc librarymembermonsieur_jj11 May '08 - 22:14 
Hi,
 
Will the distribute mfc libraries? Or even static link?
 
Thanks,
Jayjay
General2 small fixesmemberMike Melnikov8 Sep '05 - 21:13 
1. on my computer i had to change
CFtpConnection Connection2(Session,_T("ftp://ftp.codeproject.com/"));
to
CFtpConnection Connection2(Session,_T("ftp.codeproject.com/"));
 
as i was getting ERROR_INTERNET_NAME_NOT_RESOLVED
 
2. BOOL GetCreationTime(FILETIME* pTimeStamp) const
does not return value in sources

GeneralInteresting Biomembernorm.net13 Aug '05 - 1:04 
I'm still laughing now Laugh | :laugh:
 
Blogless

Generalnew wheelsmembergnk12 Aug '05 - 21:02 
Why inventing wheels? Are these classes any better than libcurl? wget? whatever???
http://curl.haxx.se/
 
gnk
GeneralRe: new wheelsmemberGilad Novik13 Aug '05 - 6:10 
Not inventing anything. I just wanted to wrap INET instead of using another library. For 99% of my needs, I don't need anything beside INET functions, so why blow your code with another library - doing the same stuff? Curl is good for multiplatform development, but since I use WTL (which is for windows only), I don't need curl.
 
Whoa! The internet is even on computers now!

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 12 Aug 2005
Article Copyright 2005 by Gilad Novik
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid