无法按照Ftp发送文件 [英] Could not send Files per Ftp

查看:65
本文介绍了无法按照Ftp发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我想通过局域网将文件发送到另一台计算机.在目标计算机上,有一个正在运行的ftp服务器.
从我的PC手动连接到ftp服务器工作正常.

因此,我尝试使用FtpWebRequest库传输文件,但始终收到错误消息:使用HTTP代理时不支持所请求的FTP命令".
我通过Google找到了一些链接,但是无论我做什么,都行不通.

这里是发送文件的方法:

Hello!

I would like to send files over the local network to an another Computer. On the destination computer, there is a ftp Server running.
Manual connecting to the ftp server from my pc works fine.

So i tried to transfer the files with the FtpWebRequest library, but i always get the error message: "The requested FTP command Is not supported when using HTTP Proxy".
I found some links via google, but whatever i do, it dont work.

Here the methods for sending the file:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostAddy);
request.Proxy = null;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("ftp://192.168.0.1","pass");
WebResponse response = request.GetResponse();

FileStream fs = new FileStream("test.txt", FileMode.Open);
byte[] fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, Convert.ToInt32(fs.Length));
fs.Flush();
fs.Close();

Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
request.Abort(); 


因此,感谢您的帮助!


So thanks for your help!

推荐答案

没错,您的代码不起作用.
您需要考虑两件事:

1)注意:您尝试使用期望usernamepasswordNetworkCredential构造函数的形式(不是URLpassword);您可以使用用户名= string.Empty .

2)另一种可能是使用URL的形式为"ftp://username:password@192.168.0.1";

3)您的服务器可能不需要密码;实际上,它可以是空字符串,也可以是除模式或URL分隔符之外的任何字符.

我认为这应该可以解决您的问题.如果还没有,则需要提供完整的转储异常信息;否则,请提供完整的转储异常信息.一个重要的项目是字符串System.Exception.Stack.

对于此类转储,您可以使用我为不同的Answer 此处 .
That''s right, your code should not work.
Two things you need to take into account:

1) Pay attention: the form of NetworkCredential constructor you''re trying to use expect username and password, (not URL and password); you can use username = string.Empty.

2) Another possibility might be using URL in the form "ftp://username:password@192.168.0.1";

3) Your server may require no password; in reality, it can be either empty string or just any character except schema or URL delimiters.

I think this should resolve your problem. If it does not (yet), you need to provide full dump exception information; one important item is the string System.Exception.Stack.

For such dump you can use my codelet I proposed for different Answer, here.


或者,我可以看到另一个问题.您只是在输出流中写入一些数据,而这些数据恰好是FTP流.在什么文件中?您的FTP命令在哪里?!

您正在尝试使用FTP协议,因为它是纯TCP.哦!
那么该怎么办?查看显示如何上传文件的示例:

Or, I can see one more problem. You''re just writing some data in output stream which happen to be FTP stream. In what file? Where is your FTP command?!

You''re trying to work with FTP protocol as it was plain TCP. Oh!
So what to do? Look at the sample showing how to upload the file:

using System;
using System.Net;
using System.Threading;

using System.IO;
namespace Examples.System.Net
{
    public class FtpState
    {
        private ManualResetEvent wait;
        private FtpWebRequest request;
        private string fileName;
        private Exception operationException = null;
        string status;

        public FtpState()
        {
            wait = new ManualResetEvent(false);
        }

        public ManualResetEvent OperationComplete
        {
            get {return wait;}
        }

        public FtpWebRequest Request
        {
            get {return request;}
            set {request = value;}
        }

        public string FileName
        {
            get {return fileName;}
            set {fileName = value;}
        }
        public Exception OperationException
        {
            get {return operationException;}
            set {operationException = value;}
        }
        public string StatusDescription
        {
            get {return status;}
            set {status = value;}
        }
    }
    public class AsynchronousFtpUpLoader
    {  
        // Command line arguments are two strings:
        // 1. The url that is the name of the file being uploaded to the server.
        // 2. The name of the file on the local machine.
        //
        public static void Main(string[] args)
        {
            // Create a Uri instance with the specified URI string.
            // If the URI is not correctly formed, the Uri constructor
            // will throw an exception.
            ManualResetEvent waitObject;

            Uri target = new Uri (args[0]);
            string fileName = args[1];
            FtpState state = new FtpState();
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example uses anonymous logon.
            // The request is anonymous by default; the credential does not have to be specified. 
            // The example specifies the credential only to
            // control how actions are logged on the server.

            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            // Store the request in the object that we pass into the
            // asynchronous operations.
            state.Request = request;
            state.FileName = fileName;

            // Get the event to wait on.
            waitObject = state.OperationComplete;

            // Asynchronously get the stream for the file contents.
            request.BeginGetRequestStream(
                new AsyncCallback (EndGetStreamCallback), 
                state
            );

            // Block the current thread until all operations are complete.
            waitObject.WaitOne();

            // The operations either completed or threw an exception.
            if (state.OperationException != null)
            {
                throw state.OperationException;
            }
            else
            {
                Console.WriteLine("The operation completed - {0}", state.StatusDescription);
            }
        }
        private static void EndGetStreamCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState) ar.AsyncState;

            Stream requestStream = null;
            // End the asynchronous call to get the request stream.
            try
            {
                requestStream = state.Request.EndGetRequestStream(ar);
                // Copy the file contents to the request stream.
                const int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                int readBytes = 0;
                FileStream stream = File.OpenRead(state.FileName);
                do
                {
                    readBytes = stream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                    count += readBytes;
                }
                while (readBytes != 0);
                Console.WriteLine ("Writing {0} bytes to the stream.", count);
                // IMPORTANT: Close the request stream before sending the request.
                requestStream.Close();
                // Asynchronously get the response to the upload request.
                state.Request.BeginGetResponse(
                    new AsyncCallback (EndGetResponseCallback), 
                    state
                );
            } 
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Could not get the request stream.");
                state.OperationException = e;
                state.OperationComplete.Set();
                return;
            }

        }

        // The EndGetResponseCallback method  
        // completes a call to BeginGetResponse.
        private static void EndGetResponseCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState) ar.AsyncState;
            FtpWebResponse response = null;
            try 
            {
                response = (FtpWebResponse) state.Request.EndGetResponse(ar);
                response.Close();
                state.StatusDescription = response.StatusDescription;
                // Signal the main application thread that 
                // the operation is complete.
                state.OperationComplete.Set();
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine ("Error getting response.");
                state.OperationException = e;
                state.OperationComplete.Set();
            }
        }
    }
}



来吧!您无法提交适当的密码?上面的文件上传示例来自Microsoft帮助(4.0),ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.net/html/9a8d5570-4510-75e2-0a39-cefd68f459a9.htm ;您会在同一页面上找到其他示例.



Come on! You could not file proper codelet?! The above file upload sample is from Microsoft help (4.0), ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.net/html/9a8d5570-4510-75e2-0a39-cefd68f459a9.htm; you''ll find other samples on the same page.


这篇关于无法按照Ftp发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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