开发者

System.Net.FtpWebRequest GetDateTimestamp call dies

开发者 https://www.devze.com 2022-12-15 22:39 出处:网络
Following test works... public void test1() { string server=\"ftp://myserver.com/dev\"; string userName=\"myusername\";

Following test works...

  public void test1()
  {
     string server="ftp://myserver.com/dev";
     string userName="myusername";
     string password="mypassword";

     FtpWebRequest req = (FtpWebRequest)WebRequest.Create( server );
     req.Credentials = new NetworkCredential( userName, password );
     req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
     req.Timeout = 30000;
     req.UseBinary = false;
     req.EnableSsl = false;
     req.UsePassive = false;
     req.KeepAlive = true;

     using( FtpWebResponse resp = (FtpWebResponse)req.GetResponse() )
     {
        using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        {
           string fileRecord = sr.ReadLine();
           while (fileRecord != null)
           {
              Console.WriteLine( fileRecord );
              fileRecord = sr.ReadLine();
           }
        }
     }
  }

While the following test fails...

  public void test2()
  {
     string server="ftp://myserver.com/dev";
     string userName="myusername";
     string password="mypassword";

     FtpWebRequest req = (FtpWebRequest)WebRequest.Create( server );
     req.Credentials = new NetworkCredential( userName, password );
     req.Method = WebRequestMethods.Ftp.GetDateTimestamp;
     req.Timeout = 30000;
   开发者_如何学Go  req.UseBinary = false;
     req.EnableSsl = false;
     req.UsePassive = false;
     req.KeepAlive = true;

     using( FtpWebResponse resp = (FtpWebResponse)req.GetResponse() )
     {
        using( StreamReader sr = new StreamReader( resp.GetResponseStream() ) )
        {
           Console.WriteLine( resp.LastModified );
        }
     }
  }

with error message:

Test method test2 threw exception: System.Net.WebException: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

UPDATE: I tried with another ftp site (unix) that uses the default port#, so the url is "ftp://myserver.com/dev" - and the GetDateTimestamp() still dies with the same error.

I have updated the subject line and the body of the question to reflect my query properly.


Please add more information.

Guess so far: You are trying to

  • a) Do a ls on the FTP server (works)
  • b) Get at timestamp from the FTP server (doesn't work)

Since everything else seems the same (address etc) I assume that both look at the same data. I would imagine that an ls just works when you're connected. But what timestamp are you trying to get there? The documentation for WebRequestMethods.Ftp.GetDateTimestamp says

Represents the FTP MDTM protocol method that is used to retrieve the date-time stamp from a file on an FTP server.

(Emphasis by me)

Which file? As far as I can see you are only specifying a folder (not sure if that works)? Did you try this with "ftp://myserver.com/dev/text.txt"?


It looks like the URI in the 2 examples are the same, your test cases don't match the description of the problem. Can you be more specific?

(In general its easiest to add have a single snippet of example code with the one or two lines that are breaking highlighted.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号