SAMPLE: FTP with CERN-Based Password Protected Proxy Using WinInet

ID: Q216214


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 3.0, 4.0


SUMMARY

ProxyFtp.exe is a sample that demontrates how to use the InternetOpenUrl WinInet API to send an FTP request through an HTTP type (that is, CERN) proxy that may require user authentication.


MORE INFORMATION

The following files are available for download from the Microsoft Download Center. Click the file names below to download the files:

ProxyFtp.exe
For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center. WinInet uses the HTTP protocol to communicate with a proxy, but then the proxy connects to the FTP server via FTP protocol. This fact makes it impossible to use specific WinInet FTP APIs such as FtpOpenFile and FtpPutFile.

However, it is still possible to obtain a directory listing of the FTP server and download files using InternetOpenUrl. This protocol-independent API is capable of taking both an FTP URL, such as ftp://server, or an HTTP URL, such as http://server.

Under certain conditions, in addition to the FTP server requiring a set of credentials (that is, the user name and password), the proxy server may require a separate set of credentials. With InternetOpenUrl the user name and password required by the FTP server may be include in the URL, like this:

ftp://User:Pass@ftp.server.com/file.txt 

NOTE: This syntax is invalid for HTTP and does not allow a password to be included with the "@" sign.

If you do not specify the FTP user name and password in the URL, the proxy accesses the server with user "Anonymous" and some sort of generic password (for example, Microsoft Proxy uses "proxyuser@microsoft.com" as the password).

The technique below outlines steps that can be used to handle proxy authentication. In other words it explains how to submit a second set of credentials for the proxy itself.

  1. Call InternetOpenUrl. If the FTP server requires a name and password, include it in the URL.


  2. Check if the handle returned by InternetOpenUrl is of type HTTP. If the handle type is HTTP, it indicates that WinInet is communicating with HTTP type proxy.


  3. Check for the HTTP status code. If status code indicates that proxy authentication is required, acquire user credentials.


  4. The user name and password may be acquired and set with a standard user interface by calling InternetErrorDlg. If you do not want to use the standard user interface, you can just use InternetSetOption to set the user name and password.


  5. Once credentials are acquired, the request must be resubmitted with the HttpSendRequest API.



Notes

  • Before the request is resubmitted, all outstanding data must be read with the InternetReadFile function.


  • When InternetReadFile is used to obtain a directory listing (for a URL such as ftp://server/MyDir), it may fail with error 122 "Insufficient buffer." In this case the forth parameter to the function, lpNumberOfBytesRead, will be set to 0 and will not indicate the size of the needed buffer. To determine the size of the minimal buffer to allocate, call the InternetQueryDataAvailable function.


How to Run the Sample

The sample uses the INTERNET_OPEN_TYPE_PRECONFIG Internet access type. Therefore, if Internet Explorer is configured to use an HTTP proxy, the sample will use the same proxy.

Use the following to get Myfile.txt and dump it to the console by using an anonymous FTP connection:

c:> readurlftp ftp://myserver/myfile.txt con 

To peform the same as operation as above, but to save the file locally:

c:> readurlftp ftp://myserver/myfile.txt d:\temp\myfile.txt 

Use the following to get a directory listing from MyDir and dump the results to the console while using a specific user name and password required by the FTP server:

c:> readurlftp ftp://User:Pass@myserver/MyDir con 
Perform the same task as above, except use the following to use the standard user interface (for example, bring up the standard dialog box) to enter proxy credentials in addition to FTP server credentials:

c:> readurlftp ftp://myserver/MyDir con -u 


REFERENCES

Q195650 HOWTO: How to Handle Proxy Authorization with WinInet

Q166961 HOWTO: FTP with CERN-Based Proxy Using WinInet API

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Leon Braginski, Microsoft Corporation

Additional query words: InternetOpenUrl, CERN, PROXY, FTP, WinInet

Keywords : kbfile kbIE300 kbIE400
Version : WINDOWS:3.0,4.0
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.