Article ID: 166961
Article Last Modified on 7/1/2004
Get ftp://host.com/root/test.doc HTTP 1.0 FTP Request
|=========| |=============| |=============|
| |---------------->| Cern-based |--------->| |
| Client | HTTP | Proxy Server| FTP | Ftp Server |
| |<----------------| |<---------| |
|=========| |=============| |=============|
HTTP/1.0 200 <document> FTP Response
Since a CERN-based proxy server communicates with its clients in HTTP, you
cannot use the FTP set of Wininet functions if the client accesses the FTP
server through the proxy server. Instead, you should use InternetOpenUrl
and InternetReadFile to retrieve the requested data from the HTTP stream
sent by the proxy server.
CHAR strURL[]="ftp://host.com/root/test.doc";
HANDLE f; //local file handle
HINTERNET hInternetFile;
DWORD dwRead, dwWritten;
CHAR szTemp[1024];
hInternetFile = InternetOpenUrl(hInternetSession,(LPCTSTR)strURL, NULL,
0,INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE, 0);
if (hInternetFile == NULL) { do some error processing; quit; }
//download the file to c:\deleteme
f = CreateFile("c:\\deleteme", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE)
{
do some error processing; quit;
}
while (InternetReadFile(hInternetFile, (LPVOID)szTemp, 1024, &dwRead))
{
if (!dwRead) break;
WriteFile(f, (LPVOID)szTemp, dwRead, &dwWritten, NULL);
}
CloseHandle(f);
//if the strURL is a URL to a file on the ftp server, we get the file
//and store it in c:\deleteme.
//if the strURL is a URL to a directory on the ftp server, c:\deleteme
//will be a HTML page of the directory listing. You will have to parse
//the html page, and construct new URLs, and call the above code again.
When the client has direct connection to a FTP server, or is connected to
the FTP server via a TIS FTP proxy server, the communication to and from
the client is in FTP. The client can specify the INTERNET_FLAG_RAW_DATA
flag in InternetOpenUrl and then use InternetFindNextFile to get a
WIN32_FIND_DATA structure and enumerate all subdirectories and files of the
URL. The client can also use the FTP set of Wininet functions in such
situations.
CHAR strURL[]="ftp://host.com/root/test.doc";
WIN32_FIND_DATA wfd;
HINTERNET hInternetFind;
CHAR *strFileName, *strDirectoryName;
hInternetFind = InternetOpenUrl(hInternetSession,(LPCTSTR)strURL,
NULL, 0, INTERNET_FLAG_RAW_DATA| INTERNET_FLAG_RELOAD |
INTERNET_FLAG_DONT_CACHE, 0);
if (hInternetFind ==NULL) {do some error processing; quit}
if (!InternetFindNextFile (hInternetFind, &wfd))
{do some error processing; quit}
do
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
strDirectoryName = (LPCTSTR) &wfd.cFileName;
if (strDirectoryName[0] == ".")
continue;
//store the directory name and construct a URL
//to this directory so that we can use the same
//code to enumerate it later;
}
else
{
strFileName = (LPCTSTR) &wfd.cFileName;
//store the file name somewhere to get it later
//or you can get the file right here using the code
//similar to the pseudo code in the above section
//for CERN Proxy access;
}
}
while (InternetFindNextFile (hInternetFind, &wfd));
InternetCloseHandle(hInternetFind);
When the connection between the client and the FTP server is direct or via
a TIS FTP proxy, InternetOpenUrl and InternetReadFile are actually
generating FTP traffic rather than HTTP traffic.
Keywords: kbhowto KB166961