Sunday, July 4, 2010

Example of InternetOpen, InternetConnect

Example of InternetOpen, InternetConnect

const service_ftp=1
const open_type_direct=1
const ftp_transfer_type_ascii=1

'Extern is a QuickTest object to declare an external functions
Extern.Declare miclong, "InternetOpen", "wininet.dll", "InternetOpenA",_ micString, micDWord, micString, micString, micDWord
Extern.Declare miclong, "InternetConnect", "wininet.dll", "InternetConnectA",_ micLong, micString, micInteger, micString, micString, micDWord, micDWord,_ micDWord
Extern.Declare micinteger, "InternetCloseHandle", "wininet.dll",_ "InternetCloseHandle", miclong

hnd_internet=Extern.InternetOpen("QTP_FTP", open_type_direct, vbnullchar,vbnullchar,0)

If hnd_internet=0 then
msgbox "Connection is Failed - 1"
Else
msgbox "Connection is Successful - 1"
End if

hnd_connection=Extern.InternetConnect(hnd_internet, "ftp.microsoft.com", 21,"","",1,0,0)

If hnd_connection=0 then
msgbox "Connection is Failed - 2"
Else
msgbox "Connection is Successful - 2"
End If

Extern.InternetCloseHandle(hnd_connection)
Extern.InternetCloseHandle(hnd_internet)


InternetOpen function initializes the internal state of the WinInet API. The first parameter sets the user agent string that is passed to the server during each call, the second parameter determines what access will be used, either direct connection or through a proxy, the third parameter lists the names of the proxy servers to use, the fourth parameter lists the names of clients that should not use a proxy server, the fifth parameter flags for setting asynchronous requests and caching support.

InternetConnect function opens an IP port for FTP (in our case) and returns an handle for that service. It requires an handle to an open Internet session which is returned by a function called InternetOpen. The first parameter to InternetConnect is the handle returned by InternetOpen. The second parameter is the name of the server (URL or IP) to connect to. The third parameter allows you to specify the port number.
Some examples of port numbers:

INTERNET_DEFAULT_FTP_PORT Uses the default port for FTP server, port 21.
INTERNET_DEFAULT_GOPHER_PORT Uses the default port for Gopher server, port 70.
INTERNET_DEFAULT_HTTP_PORT Uses the default port for HTTP server, port 80.

Next two parameters are username and password for the server you want to connect to. Next parameter is the type of service to access. It can be INTERNET_SERVICE_FTP (1), INTERNET_SERVICE_GOPHER(2) or INTERNET_SERVICE_HTTP. Next parameter is the option specific to the service used, here we are keeping it as 0. The last parameter is the value to identify the application if we use callbacks, here we are keeping it as 0 as we are not using any callbacks.

Above in the last two line we are first closing the FTP session handle and the secondly we are closing the Internet session handle.

Also See:
QTP FTP Main Post
FTPGetFile
FTPPutFile
FTPOpenFile
FTPSetCurrentDirectory
FTPCreateDirectory
FTPRenameFile
FTPRemoveDirectory
FTPDeleteFile