Article View: borland.public.cppbuilder.nativeapi
Article #8205Re: How to download a file WITHOUT using the FTP..only through HTTP..
From: Ed Mulroy [TeamB]
Date: Mon, 15 Nov 2004 19:05
Date: Mon, 15 Nov 2004 19:05
316 lines
7671 bytes
7671 bytes
See how this code works for you. It grabs one of my pages
from the web.
If you want you can do a 'quicky' by building it from the
command line with:
bcc32 -WCR -v filename.cpp
However, some older versions of BCB don't supply the
WinInet definitions by default and if that happens to
you then do something like this:
bcc32 -WCR -v filename.cpp wininet.lib
If doing it from within the IDE, make it a console mode
app
File|New|Other (bcb6)
File|New (older versions)
double click Console Wizard
Make sure GUI is not checked or Console is checked
(VCL is not used in this demo so need not be checked)
. Ed
-------------------
#include <windows.h>
#include <wininet.h>
#include <string>
#include <cstdio>
using namespace std;
const unsigned BUFFER_SIZE = 1024;
inline const char *ProgramName()
{
char *arg0 = __argv[0];
char *s = strrchr(arg0, '\\');
if (!s)
s = strrchr(arg0, '/');
if (s)
++s;
else
s = arg0;
return s;
} // end ProgramName
inline void MsgBox(const string& title, const string& err)
{
MessageBox(
HWND_DESKTOP,
err.c_str(),
title.c_str(),
MB_OK);
} // end MsgBox
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);
} // end ShowLastError
class SessionHandle
{
private:
SessionHandle(const SessionHandle&);
operator = (const SessionHandle&);
HINTERNET handle;
public:
SessionHandle()
{
const char *pname = ProgramName();
handle =
InternetOpen(
pname,
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; }
}; // end class SessionHandle
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);
}; // end class UrlHandle
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;
} // end UrlHandle::ReadToFile
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;
} // end ReadFileViaHTTP
const char download_url[] =
"http://users.deltacomm.com/edmulroy/programg.htm";
int main()
{
printf(
"%u bytes read\n",
ReadFileViaHTTP(download_url, "temp.htm")
);
return 0;
}
-------------------
>Oren Halvani wrote:
>dear builders,
>
>can someone explain me how to download a file WITHOUT
>using the FTP protocol...in our firm we ONLY can access
>the 80port HTTP..
>
>so, my question: how can i download a file via:
>InternetOpen( ), InternetConnect, InternetOpenFile( ), etc...
>
>the below snip will download a file ONLY via the FTP...
>
>/*****************************************************/
>#include <WININET.h>
>#include <stdio.h>
>
>void ResponseInfo(char *where)
>{
> char lpBuffer[8*1024];
> DWORD Err = GetLastError();
> DWORD Llp = 8*1024;
>
> if(Err < INTERNET_ERROR_BASE)
> {
> sprintf(lpBuffer,
>"Standard Windows Error Number %i", Err);
> }
> else
> if(Err == ERROR_INTERNET_EXTENDED_ERROR)
> {
> InternetGetLastResponseInfo( &Err, lpBuffer, &Llp);
> }
> else
> {
> sprintf(lpBuffer,
>"Standard.Internet.Error.Number.%i",Err);
> };
>
> MessageBox("..", lpBuffer, where,
>MB_OK|MB_ICONINFORMATION|MB_TOPMOST);
>}
>
>void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
>{
> HINTERNET handle_Session =
>InternetOpen("My App", 0, 0, NULL, INTERNET_FLAG_ASYNC);
>
> if(!handle_Session)
>{ShowMessage("Open Session = failed !"); return;}
>
> HINTERNET handle_Connect =
>InternetConnect(handle_Session, "ftp.halvani.de",
> INTERNET_DEFAULT_FTP_PORT,
> "USER", "PASS",
> INTERNET_SERVICE_FTP, 0, 0);
> if(!handle_Connect)
> {
> InternetCloseHandle(handle_Session);
>ShowMessage("Connection = failed !");
>return;
> }
> else
> {
> if(FtpGetFile(handle_Connect,
>txtRemoteFile->Text.c_str(),
>txtLocalFile->Text.c_str(),
> FALSE, FILE_ATTRIBUTE_NORMAL,
>FTP_TRANSFER_TYPE_ASCII, 0))
> {
> ShowMessage("Download of File = successfully !");
> if(InternetCloseHandle(handle_Session))
>ShowMessage("Closing Session = successfully !");
> else ShowMessage("Closing Session = failed !");
> }
> else
> {
> ShowMessage("Sorry, couldn't connect..");
> if(InternetCloseHandle(handle_Session))
>ShowMessage("Closing Session = successfully !");
> else ShowMessage("Closing Session = failed !");
> }
> }
>/*****************************************************/
Technical Details
Message-ID:
<41990c0f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41984e1e@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 From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> Sender: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> Reply-To: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> Subject: Re: How to download a file WITHOUT using the FTP..only through HTTP.. Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 24.74.175.102 References: <41984e1e@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <41990c0f$1@newsgroups.borland.com> Date: 15 Nov 2004 12:05:35 -0700 X-Trace: newsgroups.borland.com 1100549135 207.105.83.66 (15 Nov 2004 12:05:35 -0700) Lines: 317 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail