2013-10-21 44 views
6

有人可以看看下面的代碼,告訴我我做錯了什麼。我只是兜兜,,,任何指針十分讚賞FTP上傳文件使用HTTP代理時不支持請求的FTP命令

public class FtpWebRequestUtil 
{ 
    private static string RemoteHost; 
    private static string RemoteFtpPath; 
    public static NetworkCredential Credential = new NetworkCredential(); 

    public FtpWebRequestUtil() 
    { 
    } 

    public FtpWebRequestUtil(string RemoteAddress, string RemotePath, string RemoteUser, string RemotePwd) 
    { 
     Credential.UserName = RemoteUser; 
     Credential.Password = RemotePwd; 
     RemoteHost = RemoteAddress; 
     RemoteFtpPath = RemotePath; 
    } 

    public string UploadFile(string localFilePath) 
    { 
     int startTime = Environment.TickCount; 
     // Console.WriteLine("Uploading File " + localFilePath); 
     try 
     { 
      FileInfo localFile = new FileInfo(localFilePath); //e.g.: c:\\Test.txt 
      byte[] buf = new byte[2048]; 
      int iWork; 
      string remoteFile = "ftp://" + RemoteHost + "/" + RemoteFtpPath + "/" + localFile.Name; 

      FtpWebRequest req = (FtpWebRequest) FtpWebRequest.Create(remoteFile); 
      // req.Proxy = 

      req.Credentials = Credential; 


      // FtpWebRequest req = (FtpWe 

      req.UseBinary = true; 
      req.KeepAlive = true; 
      req.Method = WebRequestMethods.Ftp.UploadFile; 
      StreamWriter myStreamWriter = new StreamWriter(req.GetRequestStream()); 
      myStreamWriter.Write(new StreamReader("TestFiles\\" + localFile.Name).ReadToEnd()); 
      myStreamWriter.Close(); 
      FtpWebResponse myFtpWebResponse = (FtpWebResponse) req.GetResponse(); 
      Console.WriteLine("Upload File Complete, status: " + myFtpWebResponse.StatusDescription); 

      myFtpWebResponse.Close(); 
      return "SUCCESS"; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There was an error connecting to the FTP Server."); 
      Console.WriteLine(ex.Message); 
      throw ex; 
     } 
     Console.WriteLine("Time taken for downloading file is " + (Environment.TickCount - startTime).ToString()); 
     return "FAILURE"; 
    } 


    ************************      ********************************* 
    FtpWebRequestUtil ftpClient = new FtpWebRequestUtil(FtpUrl, InputFolder, FtpUser, FtpPassword); 
    try 
    { 
     Thread.Sleep(5000); 
     ftpClient.UploadFile(UploadingFileName); 
       } 
     catch (Exception exception) 
     { 
      Assert.Fail(exception.Message); 
     } 
     finally 
     { 
      ftpClient = null; 
     } 
    } 
} 
+0

什麼錯誤? – HappyLee

+0

使用http代理不支持ftp命令 – vishal

回答

8
req.Proxy = new WebProxy(); // initialize this FtpWebRequest property 
+0

確定嘗試了,現在獲取的對象引用未設置爲實例): – vishal

+0

您在哪裏添加了這行代碼?發佈代碼 – HappyLee

+0

req.Proxy = new WebProxy(); req.Proxy = null; req.Credentials =憑證; //的FtpWebRequest REQ =(FtpWe req.UseBinary = TRUE; req.UsePassive = TRUE; req.KeepAlive = TRUE; 的StreamWriter myStreamWriter =新的StreamWriter(req.GetRequestStream()); – vishal

0

例外本身就是答案 - 它不支持。可能你有一些防止直接連接到FTP的HTTP代理。根據MS documentation,如果指定的代理是HTTP代理,則僅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令 - 因此UploadFile不支持。

2

事實證明,只有RETRLISTNLST方法由System.Net.FtpWebRequestHTTP代理配置支持,也不要緊,你是不是在你的代碼中設置代理:如果HTTP代理(未FTP代理服務器)在系統代理設置(例如:Internet選項\連接\局域網設置\代理服務器\爲您的局域網使用代理服務器)中配置,則嘗試上傳到FTP服務器時會出現此錯誤。

解決方法是使用IE更改系統設置以關閉使用HTTP代理。但是如果你有機會到受影響的代碼的解決方案是設置爲空請求的Proxy特性,例如:

request.Proxy = null; 
+0

應該是正確答案,也可以+1。 –