我有一段非常简单的代码。 一切都写得正确,没有拼写错误,而且我和同事一起讨价还价,但我们两个人根本就没有错误在哪里。
它会顺利地通过getRequestStream()来弹出异常。它找到文件,正确编码,但似乎无法与服务器连接。
这是代码:
public class WebRequestUploadExample
{
public void WebRequestUpload()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://xxxxx");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("xxxxx", "xxxxx");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(@"D:\ftpTest\Test\Test.txt");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
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();
}
}
答案 0 :(得分:1)
可能是您的目标服务器不支持'被动'模式。你尝试过主动模式吗? request.UsePassive = false;