none
如何使用FtpWebRequest登陆到非匿名的FTP站点? RRS feed

  • 问题

  • 假如FTP需要使用的帐号与密码分别为abc与123,我在客户端代码中就使用FtpWebRequest中的Credentials = new NetworkCredential("abc", "123");然后与FTP进行连接,但是却出错,错误说:(530)未登陆.不知道这是为什么啊,难道要用其他的方法才能登陆要非匿名的FTP站点吗?如果是,要怎么写啊?请各们高手指点一下,谢谢!!还有,专用的FTP服务器可以连通。我把代码下在下面:
            public void GetFileList()//获取文件列表
            {
              
              
                FtpWebRequest reqFtp;
                reqFtp =(FtpWebRequest)FtpWebRequest.Create(txtFtp.Text);
                reqFtp.UseBinary = true;
                reqFtp.Credentials = new NetworkCredential("abc", "123");
                reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
                
                 reqFtp.BeginGetResponse(new AsyncCallback(GetFileListCallBack), reqFtp);
                          
            }
            public void GetFileListCallBack(IAsyncResult ar)//回调函数
            {
                string[] items;
                 StringBuilder sb = new StringBuilder();
                FtpWebRequest reqFtp = (FtpWebRequest)ar.AsyncState;
                FtpWebResponse response = null;
                try
                {
                    response = (FtpWebResponse)reqFtp.EndGetResponse(ar);
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                    string line = sr.ReadLine();
                    lst.Items.Clear();
                    while (line != null)
                    {
                        sb.Append(line);
                        sb.Append("\n");
                        line = sr.ReadLine();
                    }
                    sb.Remove(sb.ToString().LastIndexOf('\n'), 1);
                    sr.Close();
                    response.Close();
                    items = sb.ToString().Split('\n');

                    foreach (string c in items)
                    {
                        lst.Items.Add(c);
                    }
                }
                catch (Exception ex)
                {
                    lstStatus.Items.Add(ex.Message );
                }
               
               
            }
         

    2009年8月19日 12:47

答案

全部回复