为什么我不能以编程方式访问FTP站点?

时间:2017-06-27 22:56:17

标签: c# .net ftp

我正在尝试编写一个程序,该程序将自动从其FTP站点下载最新的客户端文件。我可以使用FTP客户端成功访问该站点,但是当我尝试以编程方式执行此操作时,我遇到了错误。我尝试了多个FTP客户端,但我无法使用它们。

例如,我使用WinSCP访问该站点,它具有这个方便的功能,它可以生成连接到当前站点所需的代码。这是一个例子:

// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.site.com",
    PortNumber = 21,
    UserName = "username",
    Password = "password",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Your code
}

我可以逐字复制生成的代码,将其放入c#程序,然后我收到错误

  

尝试以访问权限x.x.x.x禁止的方式访问套接字:21

我有什么问题,或者客户端在他们的FTP站点上配置了哪些东西阻止了我正在尝试做的事情?

在我的结果发生了变化后,我不确切地知道是什么,错误消失了,我现在可以成功访问FTP站点。

2 个答案:

答案 0 :(得分:0)

我使用System.Net

WebClient 取得了成功
public class MyLocationManager implements LocationListener {

    private static final int LOCATION_UPDATE = 1;

    private LocationManager mLocationManager;
    private Handler mHandler;

    public MyLocationManager(Context context) {
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case LOCATION_UPDATE:
                        stopLocationUpdates();
                        return true;
                    default:
                        return false;
                }
            }
        });
    }

    public void stopLocationUpdates() {
        mLocationManager.removeUpdates(this);
        mHandler.removeMessages(LOCATION_UPDATE);
    }

    public void requestLocationUpdates(long timeOut) {
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        mHandler.sendEmptyMessageDelayed(LOCATION_UPDATE, timeOut);
    }

    @Override
    public void onLocationChanged(Location location) {
        stopLocationUpdates();
        //YOUR CODE HERE
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

确保您的应用有权访问输出位置(我的示例中为using (WebClient client = new WebClient()) { client.Credentials = new NetworkCredential("username@somedomain.com", "my-pwd"); client.DownloadFile("ftp://11.22.33.44/myfile.txt", @"C:\myfile.txt"); } )。

答案 1 :(得分:0)

您可以随时尝试System.Net中的FtpWebRequest类:

string url = "11.22.33.44";
string user = "username@somedomain.com";
string pwd = "my-pwd";
string fileName = @"C:\myfile.txt";

var request = (FtpWebRequest)WebRequest.Create(String.Format("ftp://{0}/{1}", url, filename));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(user, pwd);

using (var response = (FtpWebResponse)request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var targetStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write))
        if (responseStream != null)
            responseStream.CopyTo(targetStream);