csharp: using wininet.dll

/// 
    /// Windows Embedded CE 6.0 R3  WinInet Functions
    /// https://technet.microsoft.com/zh-cn/aa914184
    /// Windows   WinINet Functions
    /// https://technet.microsoft.com/zh-cn/windows/aa385473(v=vs.71)
    ///
    /// 
    public class NetUtil
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(int Description, int ReservedValue);

        /// 
        /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败 
        /// 
        /// 
        public static bool IsConnectInternet()
        {
            int Description = 0;
            return InternetGetConnectedState(Description, 0);
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class WIN32_FIND_DATA
        {
            public UInt32 dwFileAttributes = 0;
            public FILETIME ftCreationTme;
            public FILETIME ftLastAccessTime;
            public FILETIME ftLastWriteTime;
            public UInt32 nFileSizeHigh = 0;
            public UInt32 nFileSizeLow = 0;
            public UInt32 dwReserved0 = 0;
            public UInt32 dwReserved1 = 0;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string cFileName = null;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
            public string cAlternateFileName = null;
        };


        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class FILETIME
        {
            public int dwLowDateTime = 0;
            public int dwHighDateTime = 0;
        };

        //以下是对WININET.DLL中的重要函数的托管转换
        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool InternetGetLastResponseInfo(ref uint ulError,
         [MarshalAs(UnmanagedType.LPTStr)] string strBuffer, ref uint ulBufferLength);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr InternetOpen(string strAppName, ulong
         ulAccessType, string strProxy, string strProxyBypass, ulong ulFlags);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool InternetFindNextFile(IntPtr hFind, [In, Out] WIN32_FIND_DATA
          dirData);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr InternetConnect(IntPtr ulSession, string
         strServer, uint ulPort, string strUser, string strPassword, uint ulService, uint ulFlags,
         uint ulContext);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool InternetGetConnectedState(ref uint ulFlags, uint
         ulReserved);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool FtpSetCurrentDirectory(IntPtr ulSession, string
         strPath);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FtpFindFirstFile(IntPtr ulSession, string strPath
         , [In, Out] WIN32_FIND_DATA dirData, ulong ulFlags, ulong ulContext);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool FtpGetFile(IntPtr ulSession, string strRemoteFile,
         string strLocalFile, bool bolFailIfExist, ulong ulFlags, ulong ulInetFals, ulong
         ulContext);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool FtpPutFile(IntPtr ulSession, string strLocalFile,
         string strRemoteFile, ulong ulFlags, ulong ulContext);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool FtpDeleteFile(IntPtr ulSession, string strFileName);

        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
        public static extern bool InternetCloseHandle(IntPtr ulSession);



        /// 
        //STRUCTURE to hold the directory information - implemented as a class
        /// 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class FileData
        {
            public int fileAttributes = 0;
            // creationTime was embedded FILETIME structure
            public int creationTime_lowDateTime = 0;
            public int creationTime_highDateTime = 0;
            // lastAccessTime was embedded FILETIME structure
            public int lastAccessTime_lowDateTime = 0;
            public int lastAccessTime_highDateTime = 0;
            // lastWriteTime was embedded FILETIME structure
            public int lastWriteTime_lowDateTime = 0;
            public int lastWriteTime_highDateTime = 0;
            public int nFileSizeHigh = 0;
            public int nFileSizeLow = 0;
            public int dwReserved0 = 0;
            public int dwReserved1 = 0;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public String fileName = null;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
            public String alternateFileName = null;
        } //end of class FileDate

        /// 
        /// Class FtpDll implements the 'wininet.dll' api for FTP connection over firewalls!
        /// 
        public class FtpDll
        {
            //DECLARATIONS
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern uint SetLastError(uint uiErrorCode);
            [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
            public static extern uint GetLastError();
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool InternetGetLastResponseInfo(ref uint ulError, [MarshalAs(UnmanagedType.LPTStr)] string strBuffer, ref uint ulBufferLength);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr InternetOpen(string strAppName, ulong ulAccessType, string strProxy, string strProxyBypass, ulong ulFlags);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr InternetConnect(IntPtr ulSession, string strServer, uint ulPort, string strUser, string strPassword, uint ulService, uint ulFlags, uint ulContext);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool InternetGetConnectedState(ref uint ulFlags, uint ulReserved);
            //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            //        public static extern bool FtpGetCurrentDirectory(IntPtr ulSession, string strPath, ulong ulBuffLength);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool FtpSetCurrentDirectory(IntPtr ulSession, string strPath);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr FtpFindFirstFile(IntPtr ulSession, string strPath, [In, Out] FileData dirData, ulong ulFlags, ulong ulContext); //returns handle for InternetFindNextFile
            //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            //        public static extern bool InternetFindNextFile(ulong ulFindFirst, [In, Out] FileData dirData);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool FtpGetFile(IntPtr ulSession, string strRemoteFile, string strLocalFile, bool bolFailIfExist, ulong ulFlags, ulong ulInetFals, ulong ulContext);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool FtpPutFile(IntPtr ulSession, string strLocalFile, string strRemoteFile, ulong ulFlags, ulong ulContext);
            //        [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            //        public static extern bool FtpRenameFile(IntPtr ulSession, string strExisting, string strNew);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool FtpDeleteFile(IntPtr ulSession, string strFileName);
            [DllImport("wininet.dll", CharSet = CharSet.Auto)]
            public static extern bool InternetCloseHandle(IntPtr ulSession);

            //CONSTANTS - there are plenty more of these, only the important ones are here!
            //InternetOpen:aul_AccessType flags
            public const uint OPEN_TYPE_PRECONFIG = 0;
            public const uint OPEN_TYPE_DIRECT = 1;
            public const uint OPEN_TYPE_GATEWAY = 2;
            public const uint OPEN_TYPE_PROXY = 3;
            //Ports
            public const uint INVALID_PORT_NUMBER = 0;
            public const uint DEFAULT_FTP_PORT = 21;
            public const uint DEFAULT_GOPHER_PORT = 70;
            public const uint DEFAULT_HTTP_PORT = 80;
            public const uint DEFAULT_HTTPS_PORT = 443;
            public const uint DEFAULT_SOCKS_PORT = 1080;
            //Service/Command types
            public const uint SERVICE_FTP = 1;
            public const uint SERVICE_GOPHER = 2;
            public const uint SERVICE_HTTP = 3;
            //Internet connection flags
            public const uint CONNECTION_MODEM = 1;
            public const uint CONNECTION_LAN = 2;
            public const uint CONNECTION_PROXY = 4;
            public const uint CONNECTION_MODEM_BUSY = 8;
            //FTP transfer flags
            public const uint FTP_TRANSFER_TYPE_ASCII = 1;
            public const uint FTP_TRANSFER_TYPE_BINARY = 2;

            //VARIABLES
            IntPtr hInternetSession = new IntPtr(0); //handle to the Internet session
            IntPtr hInternetConnection = new IntPtr(0); //handle to the Internet connection
            uint intErrorValue = 0; //error value
            string strErrorText = "";
            FileData myFileData;
            bool bolDebug = true;

            public FtpDll()
            { //constructor
                myFileData = new FileData(); //instance the FileData class to hold the dir info
            }

            public void funIOpen()
            {
                string strProxy = "";
                string strProxyBypass = "";
                try
                {
                    funClearError(); //clear the 'last error' value
                    hInternetSession = FtpDll.InternetOpen("FTPTest", FtpDll.OPEN_TYPE_DIRECT, strProxy, strProxyBypass, 0);
                    if (funGetError())
                    {
                        if (intErrorValue != 127) //hum... seem to always get 127 back!?!
                            throw new Exception("funIOpen failed, error value: " + intErrorValue + " - " + strErrorText);
                    }
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public bool funIConnected()
            { //check if we are connected
                bool bolOnLine = false;
                try
                {
                    if (hInternetConnection != IntPtr.Zero)
                    { //we have an internet connection handle
                        uint uintFlags = 0;
                        funClearError(); //clear the 'last error' value
                        FtpDll.InternetGetConnectedState(ref uintFlags, 0); //ask for the connection status
                        //if (uintFlags & FtpDll.CONNECTION_LAN > 0) //so long as we have a connection value...
                        if (uintFlags > 0) //so long as we have a connection value...
                            bolOnLine = true;
                        if (funGetError())
                            throw new Exception("funIConnected failed, error value: " + intErrorValue + " - " + strErrorText);
                    }
                    else
                        throw new Exception("funIConnected failed; need to establish an Internet session first!");
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
                return bolOnLine;
            }

            public void funIConnect()
            { //connect using default values
                try
                {
                    string strHostURL = "ftp.microsoft.com";
                    string strUser = "anonymous";
                    string strPW = "[email protected]";
                    funIConnect(strHostURL, strUser, strPW);
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public void funIConnect(string strHostURL, string strUser, string strPW)
            {
                if (hInternetSession == IntPtr.Zero)
                    funIOpen(); //we always need a session first
                try
                {
                    funClearError(); //make sure we are clean before starting to connect
                    hInternetConnection = FtpDll.InternetConnect(hInternetSession, strHostURL, FtpDll.DEFAULT_FTP_PORT, strUser, strPW, FtpDll.SERVICE_FTP, 0, 0);
                    if (funGetError())
                        throw new Exception("funIConnect failed, error value: " + intErrorValue + " - " + strErrorText);
                    if (hInternetConnection == IntPtr.Zero) //if the handle is still zero....
                        throw new Exception("Failed to get a handle in funIConnect - no error code given!");
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public void funFTPChangeDir(string strDir)
            {
                try
                {
                    if (!funIConnected())
                        throw new Exception("Please connect to FTP host first!");
                    funClearError(); //clear the 'last error' value
                    FtpDll.FtpSetCurrentDirectory(hInternetConnection, strDir);
                    if (funGetError())
                        throw new Exception("funFTPChangeDir failed, error value: " + intErrorValue + " - " + strErrorText);
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public string funFTPFirstFile(string strPath)
            {
                string strFileName = "";
                try
                {
                    if (!funIConnected())
                        throw new Exception("Please connect to FTP host first!");
                    funClearError(); //clear the 'last error' value
                    FtpDll.FtpFindFirstFile(hInternetConnection, strPath, myFileData, 0, 0);
                    if (funGetError())
                        throw new Exception("funFTPFirstFile failed, error value: " + intErrorValue + " - " + strErrorText);
                    strFileName = myFileData.fileName;
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
                return strFileName;
            }

            public void funFTPGetFile(string strRemoteFile, string strLocalFile)
            {
                try
                {
                    if (!funIConnected())
                        throw new Exception("Please connect to FTP host first!");
                    funClearError(); //clear the 'last error' value
                    FtpDll.FtpGetFile(hInternetConnection, strRemoteFile, strLocalFile, false, 0, 0, 0);
                    if (funGetError())
                        throw new Exception("funFTPGet failed, error value: " + intErrorValue + " - " + strErrorText);
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public void funFTPPutFile(string strLocalFile, string strRemoteFile)
            {
                try
                {
                    if (!funIConnected())
                        throw new Exception("Please connect to FTP host first!");
                    funClearError(); //clear the 'last error' value
                    FtpDll.FtpPutFile(hInternetConnection, strLocalFile, strRemoteFile, 0, 0);
                    if (funGetError())
                        throw new Exception("funFTPPut failed, error value: " + intErrorValue + " - " + strErrorText);
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public void funFTPDeleteFile(string strFileName)
            {
                try
                {
                    if (!funIConnected())
                        throw new Exception("Please connect to FTP host first!");
                    funClearError(); //clear the 'last error' value
                    FtpDll.FtpDeleteFile(hInternetConnection, strFileName);
                    if (funGetError())
                    {
                        if (intErrorValue == 2)
                        { //special case? the function seems otherwise OK?!?!?!
                            if (bolDebug) Console.WriteLine("funFTPDeleteFile succeeded, but returned an error code 2 - ignored!");
                        }
                        else
                            throw new Exception("funFTPDeleteFile failed, error value: " + intErrorValue + " - " + strErrorText);
                    }
                }
                catch (Exception e)
                {
                    if (bolDebug) Console.WriteLine(e.Message);
                    throw e; //pass this exception back to the caller
                }
            }

            public void funFTPDisconnect()
            {
                if (hInternetSession != IntPtr.Zero)
                { //if and only if we have a session...
                    try
                    {
                        funClearError(); //clear the 'last error' value
                        FtpDll.InternetCloseHandle(hInternetSession);
                        if (funGetError())
                            throw new Exception("funFTPDisconnect failed, error value: " + intErrorValue + " - " + strErrorText);
                    }
                    catch (Exception e)
                    {
                        if (bolDebug) Console.WriteLine(e.Message);
                        throw e; //pass this exception back to the caller
                    }
                }
                hInternetSession = IntPtr.Zero; //reset this handle?
            }

            public void funClearError()
            { //clear the 'last error' value
                FtpDll.SetLastError(0);
            }

            public bool funGetError()
            { //return true if an error found
                intErrorValue = FtpDll.GetLastError();
                switch (intErrorValue)
                {
                    case 127: //ERROR_PROC_NOT_FOUND
                        strErrorText = "The specified procedure could not be found.";
                        break;
                    case 2: //ERROR_FILE_NOT_FOUND
                        strErrorText = "The system cannot find the file specified.";
                        break;
                    case 18: //ERROR_NO_MORE_FILES
                        strErrorText = "There are no more files.";
                        break;
                    case 123: //ERROR_INVALID_NAME
                        strErrorText = "The filename, directory name, or volume label syntax is incorrect.";
                        break;
                    case 12003: //ERROR_INTERNET_EXTENDED_ERROR
                        try
                        {
                            string strExtErrorText = "";//to hold the ExtErrorText
                            uint uiExtErrorTextLength = 0;//start as zero length
                            FtpDll.InternetGetLastResponseInfo(ref intErrorValue, strExtErrorText, ref uiExtErrorTextLength);
                            if (uiExtErrorTextLength > 0)
                            {//indicasting the length of the message to collect
                                strExtErrorText += new string(' ', (int)uiExtErrorTextLength);//beef up the string buffer
                                FtpDll.InternetGetLastResponseInfo(ref intErrorValue, strExtErrorText, ref uiExtErrorTextLength);
                                if (uiExtErrorTextLength > 0)
                                    strErrorText = "The server has returned an extended error: " + strExtErrorText;
                                else
                                    strErrorText = "The server has returned an extended error, but the text can not be recovered at all?!?.";
                            }
                            else
                                strErrorText = "The server has returned an extended error, but the text can not be recovered!?.";
                        }
                        catch (Exception e)
                        {
                            if (bolDebug) Console.WriteLine(e.Message);
                        }
                        break;
                    case 12111:    //ERROR_FTP_DROPPED 
                        strErrorText = "The FTP operation was not completed because the session was aborted.";
                        break;
                    case 12110: //ERROR_FTP_TRANSFER_IN_PROGRESS 
                        strErrorText = "The requested operation cannot be made on the FTP session handle because an operation is already in progress.";
                        break;
                    case 12137: //ERROR_GOPHER_ATTRIBUTE_NOT_FOUND 
                        strErrorText = "The requested attribute could not be located.";
                        break;
                    case 12132: //ERROR_GOPHER_DATA_ERROR 
                        strErrorText = "An error was detected while receiving data from the Gopher server.";
                        break;
                    case 12133: //ERROR_GOPHER_END_OF_DATA 
                        strErrorText = "The end of the data has been reached.";
                        break;
                    case 12135: //ERROR_GOPHER_INCORRECT_LOCATOR_TYPE 
                        strErrorText = "The type of the locator is not correct for this operation.";
                        break;
                    case 12134: //ERROR_GOPHER_INVALID_LOCATOR 
                        strErrorText = "The supplied locator is not valid.";
                        break;
                    case 12131: //ERROR_GOPHER_NOT_FILE 
                        strErrorText = "The request must be made for a file locator.";
                        break;
                    case 12136: //ERROR_GOPHER_NOT_GOPHER_PLUS 
                        strErrorText = "The requested operation can only be made against a Gopher+ server, or with a locator that specifies a Gopher+ operation.";
                        break;
                    case 12130: //ERROR_GOPHER_PROTOCOL_ERROR 
                        strErrorText = "An error was detected while parsing data returned from the Gopher server.";
                        break;
                    case 12138: //ERROR_GOPHER_UNKNOWN_LOCATOR 
                        strErrorText = "The locator type is unknown.";
                        break;
                    case 12151: //ERROR_HTTP_DOWNLEVEL_SERVER
                        strErrorText = "The server did not return any headers.";
                        break;
                    case 12155: //ERROR_HTTP_HEADER_ALREADY_EXISTS
                        strErrorText = "The header could not be added because it already exists.";
                        break;
                    case 12150: //ERROR_HTTP_HEADER_NOT_FOUND
                        strErrorText = "The requested header could not be located.";
                        break;
                    case 12153: //ERROR_HTTP_INVALID_HEADER
                        strErrorText = "The supplied header is invalid.";
                        break;
                    case 12154: //ERROR_HTTP_INVALID_QUERY_REQUEST
                        strErrorText = "The request made to HttpQueryInfo is invalid.";
                        break;
                    case 12156: //ERROR_HTTP_REDIRECT_FAILED
                        strErrorText = "The redirection failed because either the scheme changed (HTTP to FTP) or all attempts made to redirect failed.";
                        break;
                    case 12168: //ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION
                        strErrorText = "The redirection requires user confirmation.";
                        break;
                    case 12152: //ERROR_HTTP_INVALID_SERVER_RESPONSE
                        strErrorText = "The server response could not be parsed.";
                        break;
                    case 12047: //ERROR_INTERNET_ASYNC_THREAD_FAILED
                        strErrorText = "The application could not start an asynchronous thread.";
                        break;
                    case 12010: //ERROR_INTERNET_BAD_OPTION_LENGTH
                        strErrorText = "The length of an option supplied to InternetQueryOption or InternetSetOption is incorrect for the type of option specified.";
                        break;
                    case 12166: //ERROR_INTERNET_BAD_AUTO_PROXY_SCRIPT
                        strErrorText = "There was an error in the automatic proxy configuration script.";
                        break;
                    case 12022: //ERROR_INTERNET_BAD_REGISTRY_PARAMETER
                        strErrorText = "A required registry value was located but is an incorrect type or has an invalid value.";
                        break;
                    case 12029: //ERROR_INTERNET_CANNOT_CONNECT
                        strErrorText = "The attempt to connect to the server failed.";
                        break;
                    case 12042: //ERROR_INTERNET_CHG_POST_IS_NON_SECURE
                        strErrorText = "The application is posting and attempting to change multiple lines of text on a server that is not secure.";
                        break;
                    case 12044: //ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED
                        strErrorText = "The server is requesting client authentication.";
                        break;
                    case 12046: //ERROR_INTERNET_CLIENT_AUTH_NOT_SETUP
                        strErrorText = "Client authorization is not set up on this computer.";
                        break;
                    case 12030: //ERROR_INTERNET_CONNECTION_ABORTED
                        strErrorText = "The connection with the server has been terminated.";
                        break;
                    case 12031: //ERROR_INTERNET_CONNECTION_RESET
                        strErrorText = "The connection with the server has been reset.";
                        break;
                    case 12049: //ERROR_INTERNET_DIALOG_PENDING
                        strErrorText = "Another thread has a password dialog in progress.";
                        break;
                    case 12163: //ERROR_INTERNET_DISCONNECTED
                        strErrorText = "The Internet connection has been lost.";
                        break;
                    case 12171: //ERROR_INTERNET_FAILED_DUETOSECURITYCHECK
                        strErrorText = "The function failed due to a security check.";
                        break;
                    case 12032: //ERROR_INTERNET_FORCE_RETRY
                        strErrorText = "The Win32 Internet function needs to redo the request.";
                        break;
                    case 12036: //ERROR_INTERNET_HANDLE_EXISTS
                        strErrorText = "The request failed because the handle already exists.";
                        break;
                    case 12039: //ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR
                        strErrorText = "The application is moving from a non-SSL to an SSL connection because of a redirect.";
                        break;
                    case 12040: //ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR
                        strErrorText = "The application is moving from an SSL to an non-SSL connection because of a redirect.";
                        break;
                    case 12027: //ERROR_INTERNET_INCORRECT_FORMAT
                        strErrorText = "The format of the request is invalid.";
                        break;
                    case 12019: //ERROR_INTERNET_INCORRECT_HANDLE_STATE
                        strErrorText = "The requested operation cannot be carried out because the handle supplied is not in the correct state.";
                        break;
                    case 12018: //ERROR_INTERNET_INCORRECT_HANDLE_TYPE
                        strErrorText = "The type of handle supplied is incorrect for this operation.";
                        break;
                    case 12014: //ERROR_INTERNET_INCORRECT_PASSWORD
                        strErrorText = "The request to connect and log on to an FTP server could not be completed because the supplied password is incorrect.";
                        break;
                    case 12013: //ERROR_INTERNET_INCORRECT_USER_NAME
                        strErrorText = "The request to connect and log on to an FTP server could not be completed because the supplied user name is incorrect.";
                        break;
                    case 12053: //ERROR_INTERNET_INSERT_CDROM
                        strErrorText = "The request requires a CD-ROM to be inserted in the CD-ROM drive to locate the resource requested.";
                        break;
                    case 12004: //ERROR_INTERNET_INTERNAL_ERROR
                        strErrorText = "An internal error has occurred.";
                        break;
                    case 12045: //ERROR_INTERNET_INVALID_CA
                        strErrorText = "The function is unfamiliar with the Certificate Authority that generated the server's certificate.";
                        break;
                    case 12016: //ERROR_INTERNET_INVALID_OPERATION
                        strErrorText = "The requested operation is invalid.";
                        break;
                    case 12009: //ERROR_INTERNET_INVALID_OPTION
                        strErrorText = "A request to InternetQueryOption or InternetSetOption specified an invalid option value.";
                        break;
                    case 12033: //ERROR_INTERNET_INVALID_PROXY_REQUEST
                        strErrorText = "The request to the proxy was invalid.";
                        break;
                    case 12005: //ERROR_INTERNET_INVALID_URL
                        strErrorText = "The URL is invalid.";
                        break;
                    case 12028: //ERROR_INTERNET_ITEM_NOT_FOUND
                        strErrorText = "The requested item could not be located.";
                        break;
                    case 12015: //ERROR_INTERNET_LOGIN_FAILURE
                        strErrorText = "The request to connect and log on to an FTP server failed.";
                        break;
                    case 12041: //ERROR_INTERNET_MIXED_SECURITY
                        strErrorText = "The content is not entirely secure. Some of the content being viewed may have come from unsecured servers.";
                        break;
                    case 12007: //ERROR_INTERNET_NAME_NOT_RESOLVED
                        strErrorText = "The server name could not be resolved.";
                        break;
                    case 12034: //ERROR_INTERNET_NEED_UI
                        strErrorText = "A user interface or other blocking operation has been requested.";
                        break;
                    case 12025: //ERROR_INTERNET_NO_CALLBACK
                        strErrorText = "An asynchronous request could not be made because a callback function has not been set.";
                        break;
                    case 12024: //ERROR_INTERNET_NO_CONTEXT
                        strErrorText = "An asynchronous request could not be made because a zero context value was supplied.";
                        break;
                    case 12023: //ERROR_INTERNET_NO_DIRECT_ACCESS
                        strErrorText = "Direct network access cannot be made at this time.";
                        break;
                    case 12020: //ERROR_INTERNET_NOT_PROXY_REQUEST
                        strErrorText = "The request cannot be made via a proxy.";
                        break;
                    case 12017: //ERROR_INTERNET_OPERATION_CANCELLED
                        strErrorText = "The operation was canceled, usually because the handle on which the request was operating was closed before the operation completed.";
                        break;
                    case 12011: //ERROR_INTERNET_OPTION_NOT_SETTABLE
                        strErrorText = "The requested option cannot be set, only queried.";
                        break;
                    case 12001: //ERROR_INTERNET_OUT_OF_HANDLES
                        strErrorText = "No more handles could be generated at this time.";
                        break;
                    case 12043: //ERROR_INTERNET_POST_IS_NON_SECURE
                        strErrorText = "The application is posting data to a sever that is not secure.";
                        break;
                    case 12008: //ERROR_INTERNET_PROTOCOL_NOT_FOUND
                        strErrorText = "The requested protocol could not be located.";
                        break;
                    case 12165: //ERROR_INTERNET_PROXY_SERVER_UNREACHABLE
                        strErrorText = "The designated proxy server cannot be reached.";
                        break;
                    case 12048: //ERROR_INTERNET_REDIRECT_SCHEME_CHANGE
                        strErrorText = "The function could not handle the redirection, because the scheme changed (HTTP to FTP).";
                        break;
                    case 12021: //ERROR_INTERNET_REGISTRY_VALUE_NOT_FOUND
                        strErrorText = "A required registry value could not be located.";
                        break;
                    case 12026: //ERROR_INTERNET_REQUEST_PENDING
                        strErrorText = "The required operation could not be completed because one or more requests are pending.";
                        break;
                    case 12038: //ERROR_INTERNET_SEC_CERT_CN_INVALID
                        strErrorText = "SSL certificate common name (host name field) is incorrect. eg if you entered www.server.com and the common name on the certificate says www.different.com.";
                        break;
                    case 12037: //ERROR_INTERNET_SEC_CERT_DATE_INVALID
                        strErrorText = "SSL certificate date that was received from the server is bad. The certificate is expired.";
                        break;
                    case 12170: //ERROR_INTERNET_SEC_CERT_REVOKED
                        strErrorText = "SSL certificate was revoked.";
                        break;
                    case 12169: //ERROR_INTERNET_SEC_INVALID_CERT
                        strErrorText = "SSL certificate is invalid.";
                        break;
                    case 12157: //ERROR_INTERNET_SECURITY_CHANNEL_ERROR
                        strErrorText = "The application experienced an internal error loading the SSL libraries.";
                        break;
                    case 12164: //ERROR_INTERNET_SERVER_UNREACHABLE
                        strErrorText = "The Web site or server indicated is unreachable.";
                        break;
                    case 12012: //ERROR_INTERNET_SHUTDOWN
                        strErrorText = "The Win32 Internet function support is being shut down or unloaded.";
                        break;
                    case 12159: //ERROR_INTERNET_TCPIP_NOT_INSTALLED
                        strErrorText = "The required protocol stack is not loaded and the application cannot start WinSock.";
                        break;
                    case 12002: //ERROR_INTERNET_TIMEOUT
                        strErrorText = "The request has timed out.";
                        break;
                    case 12158: //ERROR_INTERNET_UNABLE_TO_CACHE_FILE
                        strErrorText = "The function was unable to cache the file.";
                        break;
                    case 12167: //ERROR_INTERNET_UNABLE_TO_DOWNLOAD_SCRIPT
                        strErrorText = "The automatic proxy configuration script could not be downloaded. The INTERNET_FLAG_MUST_CACHE_REQUEST flag was set.";
                        break;
                    case 12006: //ERROR_INTERNET_UNRECOGNIZED_SCHEME
                        strErrorText = "The URL scheme could not be recognized, or is not supported.";
                        break;
                    default:
                        strErrorText = "Unknown error code: " + intErrorValue;
                        break;
                }
                return (intErrorValue != 0);
            }
        } //end of class FtpDll


        /*
         
         try {
                TestFtpDll myTest = new TestFtpDll();
                FtpDll myFtpDll = new FtpDll(); //instance the FtpDll class to access the FTP functions
                myFtpDll.funIOpen();
                Console.WriteLine("Connecting...");
                myFtpDll.funIConnect(); //use default connection parameters
                Console.WriteLine("Change dir to developr...");
                myFtpDll.funFTPChangeDir("developr");
                Console.WriteLine("Reading first file name in this dir...");
                string strFileName = myFtpDll.funFTPFirstFile("*.txt");
                Console.WriteLine(strFileName + " found, now uploading...");
                myFtpDll.funFTPGetFile(strFileName, strFileName);
//                if (new System.IO.FileInfo(strFileName).Exists) {
//                    Console.WriteLine("Now deleting " + strFileName + " from dir outgoing...");
//                    myFtpDll.funFTPDeleteFile(strFileName);
//                }
//                else 
//                    Console.WriteLine("Local copy of the file not found!");
                Console.WriteLine("Disconnecting...");
                myFtpDll.funFTPDisconnect();
                Console.WriteLine("Press N-I key to continue...");
                Console.ReadLine();
            
                myFtpDll.funIOpen();
                Console.WriteLine("Connecting...");
                myFtpDll.funIConnect();
                Console.WriteLine("Change dir to developr...");
                myFtpDll.funFTPChangeDir("developr");
                Console.WriteLine("Now sending test.txt to this dir...");
                myFtpDll.funFTPPutFile("test.txt", "test.txt");
                Console.WriteLine("Disconnecting...");
                myFtpDll.funFTPDisconnect();
                Console.WriteLine("Press N-I key to continue...");
                Console.ReadLine();
            }
            catch (Exception e) {
                Console.WriteLine("Failed..." + e.Message);
                Console.WriteLine("Press N-I key to continue...");
                Console.ReadLine();
            }
         */
    }

 

你可能感兴趣的:(csharp: using wininet.dll)