[c#]Use wininet.dll to realize ftp function-Part One :function list

类别:编程语言 点击:0 评论:0 推荐:
前段时间研究FTP,利用FTPClient在进行FTP文件传输时如果FTP站点不支持PASV模式,而内网对PORT模式也有限制的时候,多次测试未能成功,转换思路,考虑用WIN API。成功地实现了用C#对WININET.DLL的平台调用,下面将里面用到的函数列表如下: //下面两个是在实现FTP过程中用到的两个数据结构 [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);   在WININET.DLL中还有很多其他功能的函数,详细内容请看: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/wininet_reference.asp

翻译了上面的函数,其实已经成功了80%,后面的工作是SO EASY,就是简单地调用,例子我后面抽时间补上,这种方式的好处是只要SERVER端开放了21端口,它就可以穿越防火墙,还是很令人激动的;不过缺点也是有的,无法实现断点续传。从MSDN的介绍上来看,WINDOWS中实现FTP的方式实际上是利用了HTTP协议,而非真正地遵循FTP协议来实现的。

本文地址:http://com.8s8s.com/it/it27201.htm