MS proxy FTP sample

Source: Internet
Author: User
Tags building windows ftp connection ftp protocol

This sample demonstrates how to use internetopenurl to access FTP Server
Through HTTP-based (I. e. CERN) proxy.

Wininet uses http protocol to communicate with the proxy, but proxy
Connects to FTP server via FTP protocol. This fact makes impossible
Use specific wininet FTP APIs such as ftpopenfile, ftpputfile, etc.

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 URL such as ftp: // server or http: // server.

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

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

Please note this syntax is invalid for HTTP and does not allow Password
To include "@" sign. The technique below outlines steps that can be used
Handle proxy authentication. In other words it explaines how
Submit second set of credentials for the proxy itself.

1. Call internetopenurl. If FTP server requires name and password,
Include it in the URL.
2. Check if handle return by internetopenurl is HTTP type of handle. If
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. user name and password may be acquired with UI by calling interneterrordlg
API or without UI by calling internetsetoption API.
5. Once credentials are acquired request must be resubmitted
Httpsendrequest API.

Notes:
1. Before Request is resubmitted all outstanding data must be read
Internetreadfile APIs
2. When internetreadfile is used to obtain a directory listing
(Ftp: // server/mydir), it may fail with error 122 "insufficient buffer". In
This case forth parameter to the APIs (lpnumberofbytesread) will be set
0 and will not indicate the size of the needed buffer. To determine the size
Of the minimal buffer to allocate call internetquerydataavailable API.

How to run the sample:
Sample uses internet_open_type_preconfig Internet access type. Therefor if
Internet Explorer is configured to use HTTP proxy, the sample will use
Same proxy.

To get myfile.txt and dump to the console by using anonymous FTP connection:
C:> readurlftp ftp: // myserver/myfile.txt con

The same as abve, but save file localy:
C:> readurlftp ftp: // myserver/myfile.txt D: \ temp \ myfile.txt

To get directory listing from mydir and dump to the console by using
Specific user name and password required by FTP Server:
C:> readurlftp ftp: // User: Pass @ myserver/mydir con

The same as abve, accept use UI (I. e. Bring up dialog box) to enter proxy
Credentials In addition to FTP server credentials:
C:> readurlftp ftp: // myserver/mydir con-u

 

 

Readurlftp. cpp

# Include <windows. h>
# Include <wininet. h>
# Include <iostream. h>

Bool Bui = false;
Bool errorout (DWORD derror, tchar * szcallfunc );
Bool getremotefile (hinternet in hopen, tchar * In szurl, tchar * In szfilename );

Int main (INT argc, char * argv [])
{

hinternet hopen;
If (argc <3)
{< br> cerr <"Usage: " cerr <" \ t URL-ftp://ftp.domain.com/file.txt " cerr <"\ t or FTP: // User: password@ftp.domain.com/file.txt" cerr <"\ t localfile-C: \ temp \ file.txt " cerr <" \ t [-u]-use UI to authenticate with proxy " return 0;
}< BR> If (argc = 4 )&&(! Strcmp (argv [3], "-U")
Bui = true;

If (! (Hopen = internetopen ("simple FTP", internet_open_type_preconfig, null, null, internet_flag_keep_connection )))
{
Errorout (getlasterror (), "internetopen ");
Return 0;
}

If (! Getremotefile (hopen, argv [1], argv [2])
{
Cerr <"getremotefile failed" <Endl;
Return 0;
}
Return 1;
}

Bool getremotefile (hinternet in hopen, tchar * In szurl, tchar * In szfilename)
{
DWORD dwlength, dwritten, dwcode, dwsize;
Security_attributes SA = {sizeof (security_attributes), null, false };
Handle hfile;
Hinternet hconnect;
Bool binitalrequest = true;
Char * szbuffer;

If (! (Hconnect = internetopenurl (hopen, szurl, null, 0,
Internet_flag_dont_cache |
Internet_flag_keep_connection |
Internet_flag_reload, 0 )))
{
Errorout (getlasterror (), text ("internetopenurl "));
Return 0;
}
Again:
If (! Binitalrequest)
{
If (! Httpsendrequest (hconnect, null, 0, null, 0 ))
{
Errorout (getlasterror (), "httpsendrequest ");
Return false;
}
}
Dwsize = sizeof (dwcode );
If (! Internetqueryoption (hconnect, internet_option_handle_type,
& Dwcode, & dwsize ))
{
Errorout (getlasterror (), "httpqueryinfo ");
Return false;
}
If (dwcode = internet_handle_type_http_request) |
(Dwcode = internet_handle_type_connect_http ))
{
// If were are here, it means that we use HTTP to talk the proxy
Dwsize = sizeof (DWORD );
If (! Httpqueryinfo (hconnect, http_query_status_code | http_query_flag_number,
& Dwcode, & dwsize, null ))
{
Errorout (getlasterror (), "httpqueryinfo ");
Return false;
}

If (dwcode = http_status_proxy_auth_req)
{< br> If (! Internetquerydataavailable (hconnect, & dwlength, 0, 0)
{< br> errout (getlasterror (), "internetquerydataavailable");
return false;
}

szbuffer = new char [dwlength];
// read all the data and ignore it
DO
{< br> internetreadfile (hconnect, szbuffer, dwlength, & dwsize);
}< br> while (dwsize! = 0);
Delete [] szbuffer;

If (! Bui)
{
// This is a secure page.
Cerr <"proxy authentication is required." <Endl;
Char szuser [50] = "";
Char szpass [50] = "";

Cerr <"User :";
Cin> szuser;
Cerr <"Pass :";
Cin> szpass;
If (! Internetsetoption (hconnect, internet_option_proxy_username, (lpvoid) szuser, lstrlen (szuser )))
{
Cerr <"internetsetoptionfailed:" <getlasterror () <Endl;
Return false;
}
If (! Internetsetoption (hconnect, internet_option_proxy_password, (lpvoid) szpass, lstrlen (szpass )))
{
Cerr <"internetsetoptionfailed:" <getlasterror () <Endl;
Return false;
}
Binitalrequest = false;
Goto again;
}
Else
{
If (interneterrordlg (getasktopwindow (),
Hconnect,
Error_internet_incorrect_password,
Flags_error_ui_filter_for_errors |
Flags_error_ui_flags_generate_data |
Flags_error_ui_flags_change_options,
Null) = error_internet_force_retry)
{
Binitalrequest = false;
Goto again;
}
} // Else
} // If (dwcode = http_status_proxy_auth_req
} // If (dwcode = internet_handle_type_http_request

If (hfile = createfile (szfilename,
Generic_write,
File_pai_read,
& SA,
Create_always,
File_attribute_normal, null) = invalid_handle_value)
{
Cerr <"can't open file:" <szfilename <Endl;
Return false;
}
If (! Internetquerydataavailable (hconnect, & dwlength, 0, 0 ))
{
Errorout (getlasterror (), "internetquerydataavailable ");
Return false;
}
Szbuffer = new char [dwlength];

Do
{
If (! Internetreadfile (hconnect, szbuffer, dwlength, & dwritten ))
{
Closehandle (hfile );
Errorout (getlasterror (), "internetreadfile ");
Delete [] szbuffer;
Return false;
}

If (! Dwritten)
Break;
Else
If (! Writefile (hfile, szbuffer, dwritten, & dwsize, null ))
{
Closehandle (hfile );
Cerr <"error" <getlasterror () <Endl;
Delete [] szbuffer;
Return false;
}
}
While (true );
Flushfilebuffers (hfile );
Closehandle (hfile );
Delete [] szbuffer;
Return 1;
}

Bool errorout (DWORD derror, tchar * szcallfunc)
{
Tchar sztemp [512] = "", * szbuffer = NULL, * szbufferfinal = NULL;
Char strname [256] = "";
DWORD dwinterror, dwlength = 0;

Formatmessage (format_message_from_hmodule,
Getmodulehandle ("wininet. dll"), derror, 0,
(Lpstr) strname, 256, null );
Wsprintf (sztemp, "% s error code: % d \ nmessage: % s \ n ",
Szcallfunc, derror, strname );
If (derror = error_internet_extended_error)
{
Internetgetlastresponseinfo (& dwinterror, null, & dwlength );
If (dwlength)
{
If (! (Szbuffer = (tchar *) localalloc (lptr, dwlength )))
{
Lstrcat (sztemp, text ("unable to allocate memory to display Internet error code. Error Code :"));
Lstrcat (sztemp, text (_ ITOA (getlasterror (), szbuffer, 10 )));
Lstrcat (sztemp, text ("\ n "));
Cerr <sztemp <Endl;
Return false;
}
If (! Internetgetlastresponseinfo (& dwinterror, (lptstr) szbuffer, & dwlength ))
{
Lstrcat (sztemp, text ("unable to get internet error. Error Code :"));
Lstrcat (sztemp, text (_ ITOA (getlasterror (), szbuffer, 10 )));
Lstrcat (sztemp, text ("\ n "));
Cerr <sztemp <Endl;
Return false;
}
If (! (Szbufferfinal = (tchar *) localalloc (lptr, (strlen (szbuffer) + strlen (sztemp) + 1 ))))
{
Lstrcat (sztemp, text ("unable to allocate memory. Error Code :"));
Lstrcat (sztemp, text (_ ITOA (getlasterror (), szbuffer, 10 )));
Lstrcat (sztemp, text ("\ n "));
Cerr <sztemp <Endl;
Return false;
}
Lstrcpy (szbufferfinal, sztemp );
Lstrcat (szbufferfinal, szbuffer );
Localfree (szbuffer );
Cerr <szbufferfinal <Endl;
Localfree (szbufferfinal );
}
}
Else
Cerr <sztemp <Endl;
Return true;
}

 

 

Makefile

# Nmake macros for building windows 32-bit apps

Targetos = both
Appver = 4.0

ALL: readurlftp.exe

! Include <win32.mak>

Lflags =/Incremental: No/PDB: NONE/release/nologo-subsystem: console, $ (appver)-entry: maincrtstartup
Readurlftp. OBJ: readurlftp. cpp
$ (CC) $ (cflags) $ (cvars) $ (cdebug) readurlftp. cpp

Readurlftp.exe: readurlftp. OBJ
$ (Link) $ (lflags)-out: readurlftp.exe readurlftp. OBJ wininet. Lib user32.lib kernel32.lib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.