联通客户端访问电信服务器访问不了的解决方案

15 篇文章 0 订阅

联通客户端访问电信服务器访问不了的解决方案:
访问FTP服务器有两种方式:port和passive,跨网络运营商访问FTP要使用:passive模式
设置ftp.UsePassive = true即可解决,我曾租用了阿里云的服务器ECS,是电信IP,电信用户正常访问,而联通用户就无法访问,总是报错,经过研究,发现就是这个ftp.UsePassive的问题,设置了ftp.UsePassive = true,但是还是无法访问,安全组中创建两个规则:20、21、1024-65535,详细看链接:
设置方法的链接1
设置方法的链接2
就能跨运营商访问FTP了!

  public void DownloadFile(string localDir, string FtpDir, string FtpFile, string hostname, string username, string password)
        {
           
            string URI = "FTP://" + hostname + "/" +  FtpDir + "/" + FtpFile;
            //string URI = "FTP://" + hostname + "/" + FtpDir + FtpFile;
            string tmpname = Guid.NewGuid().ToString();
            string localfile = localDir + @"\" + tmpname;

            System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
            ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
            ftp.UseBinary = true;
            ftp.UsePassive = true;

            using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    //loop to read & write to file
                    using (FileStream fs = new FileStream(localfile, FileMode.CreateNew))
                    {
                        try
                        {
                            byte[] buffer = new byte[2048];
                            int read = 0;
                            do
                            {
                                read = responseStream.Read(buffer, 0, buffer.Length);
                                fs.Write(buffer, 0, read);
                            } while (!(read == 0));
                            responseStream.Close();
                            fs.Flush();
                            fs.Close();
                        }
                        catch (Exception)
                        {
                            //catch error and delete file only partially downloaded
                            fs.Close();
                            //delete target file as it's incomplete
                            File.Delete(localfile);
                            throw;
                        }
                    }

                    responseStream.Close();
                }

                response.Close();
            }



            try
            {
                File.Delete(localDir + @"\" + FtpFile);
                File.Move(localfile, localDir + @"\" + FtpFile);
                

               // ftp = null;
               // ftp = GetRequest(URI, username, password);
                ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile;
               // ftp.GetResponse();

            }
            catch (Exception ex)
            {
                File.Delete(localfile);
                throw ex;
            }

            // 记录日志 "" + URI.ToString() + "下载到" + localDir + @"\" + FtpFile + "成功." );
            ftp = null;
        }

秋风写于淄博,业务联系与技术交流QQ:375172665

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值