🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8207

Re: How to download a file WITHOUT using the FTP..only through HTTP..

#8207
From: Oren Halvani
Date: Mon, 15 Nov 2004 22:28
162 lines
4919 bytes
"Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> schrieb im
 Newsbeitrag news:41990c0f$1@newsgroups.borland.com...

Ed, THANS alot for the fantastic snip..

i need a windows-interface so i've modified your
code a little and it works really SUPERB !!

can you take a last look on it if i've modified something
wrong / dangerous...?

i'm thinking here i.e.:

// ReadFileViaHTTP(const string& src_url,const string& dest_name)
{
   DWORD result = 0;
   SessionHandle session_handle;

   if(&session_handle) // <-----is this OK...?


my next step is now to throw away this WinInet.lib and
replace it with dynamicly callingns to LoadLibrary() and
GetProcAddress()

i need to make this programm 100 % standalone thats why
i can't use the .lib...


once again thanks for your great help ED :-)

Oren

/***************************************************/
using namespace std;
const unsigned BUFFER_SIZE = 1024;

inline void MsgBox(const string& title, const string& err)
{
   MessageBox(HWND_DESKTOP, err.c_str(), title.c_str(), MB_OK);
}

void ShowLastError(const string& title_text = "Error", const string& error_text
= "")
{
   if(error_text.length()) MsgBox(error_text.c_str(), title_text.c_str());
   else
   {
     TCHAR *err_msg;

     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        reinterpret_cast<LPTSTR>(&err_msg), 0, NULL);

     MsgBox(err_msg, title_text.c_str());
     LocalFree(err_msg);
   }
   SetLastError(0);
}

class SessionHandle
{

private:

   SessionHandle(const SessionHandle&);
   operator = (const SessionHandle&);

   HINTERNET handle;

public:

   SessionHandle()
   {
      handle = InternetOpen("MyTool",
     INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
      if(!handle) ShowLastError("SessionHandle");
   }

   ~SessionHandle() {if (handle) InternetCloseHandle(handle);}

   operator HINTERNET() { return handle; }
   operator bool() const { return handle != NULL; }
};


class UrlHandle
{

private:

   UrlHandle();
   UrlHandle(const UrlHandle&);
   operator = (const UrlHandle&);

   HINTERNET handle;

public:

   UrlHandle(SessionHandle& shand, const string& src_name)
   {
     handle = InternetOpenUrl(shand, src_name.c_str(), NULL, 0, 0, 0);
     if(!handle) ShowLastError("UrlHandle");
   }

   ~UrlHandle() { if (handle) InternetCloseHandle(handle); }

   operator HINTERNET()  {return handle;}
   operator bool() const  {return handle != NULL;}

   DWORD ReadToFile(const string& dest_name);
};

const unsigned URL_BUFFER_SIZE = 1024;

DWORD UrlHandle::ReadToFile(const string& dest_name)
{
   DWORD write_total = 0;
   FILE *fout = fopen(dest_name.c_str(), "wb");

   if(fout)
   {
     DWORD num_read;
     unsigned char buffer[URL_BUFFER_SIZE];

     while(1)
     {
       if(!InternetReadFile(handle, &buffer, sizeof(buffer), &num_read) ||
!num_read) break;

       fwrite(buffer, 1, num_read, fout);
       write_total += fwrite(buffer, 1, num_read, fout);
     }
     fclose(fout);
   }
   else ShowLastError("UrlHandle::ReadToFile", "fopen failed");

   return write_total;
}

DWORD WINAPI ReadFileViaHTTP(const string& src_url,
 const string& dest_name)
{
   DWORD result = 0;
   SessionHandle session_handle;

   if(&session_handle)
   {
     UrlHandle url_handle(session_handle, src_url);
     if(&url_handle) result = url_handle.ReadToFile(dest_name);
   }
   return result;
}

void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
   string url = "http://users.deltacomm.com/edmulroy/programg.htm";

   ReadFileViaHTTP(url, "temp.htm");
}
/***************************************************/
Technical Details
Message-ID: <41992dc8@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <41984e1e@newsgroups.borland.com> <41990c0f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f57c5862d590fb6d
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Reply-To: "Oren Halvani" <Spam@Me-Please.com>
From: "Oren Halvani" <Spam@Me-Please.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References:  <41984e1e@newsgroups.borland.com> <41990c0f$1@newsgroups.borland.com>
Subject: Re: How to download a file WITHOUT using the FTP..only through HTTP..
Date: Mon, 15 Nov 2004 23:28:39 +0100
Lines: 164
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 217.234.92.201
Message-ID: <41992dc8@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1100557768 217.234.92.201 (15 Nov 2004 14:29:28 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail