FTP连接 - 验证 [英] FTP Connection - verification

查看:128
本文介绍了FTP连接 - 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我需要对ftp连接进行一些验证。以下是我到目前为止保持连接处于活动状态和打开状态的原因:



Hi everyone!

I need some verification on ftp connections. Below is what I have so far to keep the connection alive and open:

FTPWebRequest request = (FtpWebRequest)WebRequest.Create("url");
request.KeepAlive = true;
request.ReadWriteTimeout = 600000;





我的目标是能够无限期地保持连接打开,直到所有文件都通过FTP成功上传。我的代码是否正确?如果我的目标不可能,那么实现这一目标的唯一方法是将readwritetimeout时间提高得非常高吗?



谢谢大家!



My goal is to be able to keep the connection open indefinitely until all of the files have been uploaded via FTP successfully. Do I have the code correct? If my goal isn't possible, is the only way to accomplish this to raise the readwritetimeout time very high?

Thanks everyone!

推荐答案

你好,



FTPWebRequest.KeepAlive 属性会让你指定一个布尔值是否控制连接到请求完成后FTP服务器关闭。



FTPWebRequest.ReadWriteTimeout 属性将允许您指定读取或写入前的毫秒数超时默认值为300,000毫秒(5分钟)。

如果您的文件非常大和/或您的互联网连接速度很慢,这可能是个问题。



您可以尝试异步上传文件。



请阅读课程文档这里





上传一个示例代码文件到ftp服务器:

Hello,

FTPWebRequest.KeepAlive property will let you specify a boolean value whether the control connection to the FTP server is closed after the request completes.

FTPWebRequest.ReadWriteTimeout property will let you specify the number of milliseconds before the reading or writing times out. The default value is 300,000 milliseconds (5 minutes).
If your file are very big and/or you have a slow internet connection this can be a problem.

You can try upload the files asynchronously.

Please read the class documentation here.


Sample code to upload a file to a ftp server:
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;

try
{
    //Settings required to establish a connection with the server
    this.ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ServerIP/FileName"));
    this.ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    this.ftpRequest.Proxy = null;
    this.ftpRequest.UseBinary = true;
    this.ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
    //Selection of file to be uploaded
    FileInfo ff = new FileInfo("File Local Path With File Name");//e.g.: c:\\Test.txt
    byte[] fileContents = new byte[ff.Length];
    //will destroy the object immediately after being used
    using (FileStream fr = ff.OpenRead())
    {
        fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
    }
    using (Stream writer = ftpRequest.GetRequestStream())
    {
        writer.Write(fileContents, 0, fileContents.Length);
    }
    //Gets the FtpWebResponse of the uploading operation
    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse(); 
    Response.Write(this.ftpResponse.StatusDescription); //Display response
}
catch (WebException webex)
{
    this.Message = webex.ToString();
}





干杯,

JAFC



Cheers,
JAFC


这篇关于FTP连接 - 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆