手机ftp远程服务器连接到服务器,到服务器的FTP请求无法连接远程服务器

我在这里发现了一些类似的文章,但仍无法解决我的问题。 我正尝试将文本文件上传到FTP服务器。我用了一些方法和所有的人我得到同样的错误:“无法连接到远程服务器”到服务器的FTP请求无法连接远程服务器

方法一: 文件名是该文件所在的完整路径

private string Upload(string ftpServer, string userName, string password, string filename)

{

string reply = "Success";

try

{

using (System.Net.WebClient client = new System.Net.WebClient()) //System.Net.WebClient client = new System.Net.WebClient()

{

client.Credentials = new System.Net.NetworkCredential(userName, password);

client.Proxy = new WebProxy();

FileInfo fi = new FileInfo(filename);

client.UploadFile(ftpServer + "//" + fi.Name, "STOR", filename);

}

}

catch (Exception ex)

{

reply = ex.Message;

}

return reply;

}

方法2:

文件名= “d:\文件夹\ file.txt的” 公共静态无效uploadFileUsingFTP(字符串文件名) { 的FileInfo fileInf =新的FileInfo(文件名); string uri =“ftp://”+ serverIP +“/”+ fileInf.Name;

FtpWebRequest reqFTP;

// Create FtpWebRequest object from the Uri provided

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

reqFTP.Proxy = null;

// Provide the WebPermission Credintials

reqFTP.Credentials = new NetworkCredential(user, pass);

// By default KeepAlive is true, where the control connection is not closed

// after a command is executed.

reqFTP.KeepAlive = false;

// Specify the command to be executed.

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// Specify the data transfer type.

reqFTP.UseBinary = true;

// Notify the server about the size of the uploaded file

FileStream fs = File.OpenRead(filename);

reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb

int buffLength = Convert.ToInt32(fs.Length);

byte[] buff = new byte[buffLength];

int contentLen;

try

{

// Stream to which the file to be upload is written

Stream strm = reqFTP.GetRequestStream();

// Read from the file stream 2kb at a time

contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends

while (contentLen != 0)

{

// Write Content from the file stream to the FTP Upload Stream

strm.Write(buff, 0, contentLen);

contentLen = fs.Read(buff, 0, buffLength);

}

// Close the file stream and the Request Stream

strm.Close();

fs.Close();

}

catch (Exception ex)

{

string s = ex.Message;

}

}

方法3:

public static void Sample(string filename)

{

// Get the object used to communicate with the server.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serverip/"); //test.htm

request.Method = WebRequestMethods.Ftp.UploadFile;

// This example assumes the FTP site uses anonymous logon.

request.Credentials = new NetworkCredential (user,passs);

try

{

// Copy the contents of the file to the request stream.

StreamReader sourceStream = new StreamReader(filename);

byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

sourceStream.Close();

request.ContentLength = fileContents.Length;

request.Proxy = null;

Stream requestStream = request.GetRequestStream();

requestStream.Write(fileContents, 0, fileContents.Length);

requestStream.Close();

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

response.Close();

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

使用他们每个人产生同样的问题,是的,我能够通过使用FileZilla的连接到FTP,并传输文件。

我知道我必须失去一些非常愚蠢的东西,但是这让我感到非常沮丧。 任何建议都会被处理。

2012-09-30

Blerta

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值