繁体   English   中英

使用 WebClient 无法连接到远程服务器

[英]using WebClient getting unable to connect to remote server

我已经尝试了以下我能想到的所有变体。

client.Credentials = new NetworkCredential(ftpInfo.ftpUserName, ftpInfo.ftpPassWord);
client.BaseAddress = "ftp://99.999.9.99";
var response = client.UploadFile("testFile.txt", "C:\\ftproot\\testfile\\012\\Drop\\testFile.txt");

我知道用户名和密码是正确的。 如果我从同一个盒子使用 filezilla 连接到服务器,它就可以工作。

我试过不在上面使用 ftp:// - 我必须遗漏一些非常简单的东西。

这是错误:{“无法连接到远程服务器”}

  • 响应 {System.Net.FtpWebResponse} System.Net.WebResponse {System.Net.FtpWebResponse}
  • ContentType '($exception).Response.ContentType' 抛出类型为 'System.NotImplementedException' 的异常 string {System.NotImplementedException}

更新:我不知道这个问题有什么问题。 我已经提供了尽可能多的信息。

这是使用注释中的一些建议进行的当前测试。

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("password", "loginname");
    client.UploadFile("ftp://99.999.6.130/testFile.txt", "STOR", "c:\\testfile.txt");
} 

那只是说明我没有登录。

下面的工作......我会在允许时关闭问题。

结局更新——工作解决方案:

public static bool UploadFile(string url, string userName, string password, string file, out string statusDescription)
{
    try
    {
        var request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(userName, password);

        // Copy the entire contents of the file to the request stream.
        var sourceStream = new StreamReader(file);
        var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;
        var getResponse = request.GetResponse();

        Console.WriteLine($"{fileContents.Length} {getResponse} ");
    }
}

以下是一个可行的解决方案。

public static bool UploadFile(string url, string userName, string password, string file, out string statusDescription)
{
    try
    {
        var request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(userName, password);

        // Copy the entire contents of the file to the request stream.
        var sourceStream = new StreamReader(file);
        var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;
        var getResponse = request.GetResponse();

        Console.WriteLine($"{fileContents.Length} {getResponse} ");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM