我在本地做一个网站,其中要向外网的的ftp服务器上传文件,在局域网内没有问题,向外网传输是报:
System.Net.WebException: 基础连接已经关闭: 服务器提交了协议冲突。
   在 System.Net.FtpWebRequest.CheckError()
   在 System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   在 System.Net.FtpWebRequest.RequestCallback(Object obj)
   在 System.Net.CommandStream.InvokeRequestCallback(Object obj)
   在 System.Net.CommandStream.Abort(Exception e)
   在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   在 System.Net.FtpWebRequest.GetResponse()
   在 _Default.Upload(String filename) 位置 e:\workeWorke\FtpShangChuan\Default.aspx.cs:行号 53
我的代码是: public void Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能
    {        FileInfo fileInf = new FileInfo(filename);        string uri = "ftp://192.168.1.116/Aba_Zixun_Kejiqianyan_2.html"
        Connect(uri);//连接          
        // 默认为true,连接不会被关闭
        // 在一个命令之后被执行        reqFTP.KeepAlive = true;
        // 指定执行什么命令        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        // 上传文件时通知服务器文件的大小        reqFTP.ContentLength = fileInf.Length;
        // 缓冲大小设置为kb 
        reqFTP.EnableSsl = true;
        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;
        // 打开一个文件流(System.IO.FileStream) 去读上传的文件
        FileStream fs = fileInf.OpenRead();
        try
        { 
            WebResponse web=reqFTP.GetResponse();
            
            // 把上传的文件写入流  
           
            Stream strm = reqFTP.GetRequestStream();
            
            // 每次读文件流的kb 
            contentLen = fs.Read(buff, 0, buffLength);
            // 流内容没有结束
            while (contentLen != 0)
            {
                // 把内容从file stream 写入upload stream 
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }
            // 关闭两个流
            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }    }
    
    //连接ftp
    private void Connect(String path)
    {
        // 根据uri创建FtpWebRequest对象
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
        // 指定数据传输类型
        reqFTP.UseBinary = true;
        // ftp用户名和密码
        reqFTP.Credentials = new NetworkCredential("user", "62500");    }
谢谢

解决方案 »

  1.   

    楼主,这是.Net的bug,恐怕解决不了了。试试在程序头部调用下面这个函数:public static bool SetAllowUnsafeHeaderParsing20()
    {
      //Get the assembly that contains the internal class
      Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
      if (aNetAssembly != null)
      {
        //Use the assembly in order to get the internal type for the internal class
        Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (aSettingsType != null)
        {
          //Use the internal static property to get an instance of the internal settings class.
          //If the static instance isn't created allready the property will create it for us.
          object anInstance = aSettingsType.InvokeMember("Section",
            BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });      if (anInstance != null)
          {
            //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
            FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
            if (aUseUnsafeHeaderParsing != null)
            {
              aUseUnsafeHeaderParsing.SetValue(anInstance, true);
              return true;
            }
          }
        }
      }
      return false;
    }
      

  2.   

    估计你的IE设置了代理!在代码中加入 限制访问时候使用代理
    listRequest.Proxy = null;
      

  3.   

    估计你的IE设置了代理!在代码中加入 限制访问时候使用代理
    listRequest.Proxy = null;
      

  4.   

    估计你的IE设置了代理!在代码中加入 限制访问时候使用代理
    listRequest.Proxy = null;