Пример #1
0
        /// <summary>
        /// 发送测试FTP请求
        /// </summary>
        private async void SendFTPRequest()
        {
            IPAddress address = null;

            try//检测IP是否合法
            {
                address = IPAddress.Parse(model.SiteAddress);
            }
            catch
            {
                await new MessageDialog("wrong address").ShowAsync();
                return;
            }
            IdentificationInfo userInfo = new IdentificationInfo();//用户输入信息

            userInfo.Username = model.Username;
            userInfo.Password = model.Password;
            LoginType loginType = LoginType.Anonymous;

            if (model.NeedUser)//检测登录方式
            {
                loginType = LoginType.Identify;
            }
            FTPRequest request = new FTPRequest(loginType);

            request.Identification = userInfo;
            request.IdentifyType   = loginType;
            request.FtpServer      = address;
            await request.MakeRequest();

            await new MessageDialog(request.ProtocalInfo).ShowAsync();//显示返回信息
        }
 [TestInitialize()] // 测试类生成预处理
 public void Initialize()
 {
     request = new FTPRequest(LoginType.Identify)
     {
         FtpServer = IPAddress.Parse(TestFTPIP)
     };
 }
Пример #3
0
 /// <summary>
 /// 发送测试FTP请求
 /// </summary>
 private async void SendFTPRequest()
 {
     IPAddress address = null;
     try
     {
         Task<IPAddress> getAddress = util.GetIPAddressAsync(model.SiteAddress);//获取IP地址
         await getAddress;
         address = getAddress.Result;
     }
     catch//域名解析失败
     {
         await new MessageDialog("The domain name is not exist ").ShowAsync();
         return;
     }
     if (address == null)//如果IPAddress为null说明域名错误
     {
         await new MessageDialog("The domain name is wrong ").ShowAsync();
         return;
     }
     IdentificationInfo userInfo = new IdentificationInfo();//用户输入信息
     userInfo.Username = model.Username;
     userInfo.Password = model.Password;
     LoginType loginType = LoginType.Anonymous;
     if (model.NeedUser)//检测登录方式
     {
         loginType = LoginType.Identify;
     }
     FTPRequest request = new FTPRequest(loginType);
     request.Identification = userInfo;
     request.IdentifyType = loginType;
     request.FtpServer = address;
     await request.MakeRequest();
     await new MessageDialog(request.ProtocalInfo).ShowAsync();//显示返回信息
 }
 public FTPRequest(FTPRequest request)
 {
     this.Server      = request.Server;
     this.UserName    = request.UserName;
     this.Pwd         = request.Pwd;
     this.BasePath    = request.BasePath;
     this.LocalFolder = request.LocalFolder;
 }
        /// <summary>
        /// 请求FTP服务器的状态 创建者: xb 创建时间:2018/05/10
        /// </summary>
        /// <param name="site">待请求的站点</param>
        /// <param name="request">请求对象</param>
        public async Task <LogModel> AccessFTPServer(SiteModel site, FTPRequest request)
        {
            #region 初始化log
            LogModel log = new LogModel
            {
                Site_id     = site.Id,
                Create_Time = DateTime.Now
            };
            #endregion
            if (null != site.Site_address && !("".Equals(site.Site_address)))
            {
                // 检测并赋值DNS服务器IP
                IPAddress ip = await GetIPAddressAsync(site.Site_address);

                // IP 不合法
                if (null == ip)
                {
                    log.TimeCost    = 7500;
                    log.Status_code = "1001";
                    log.Is_error    = true;
                    log.Log_Record  = "Address Format Is Invalid!";
                }
                else
                {
                    request.FtpServer = IPAddress.Parse(site.Site_address);
                    // 获取保存的用户验证的信息
                    JObject js = (JObject)JsonConvert.DeserializeObject(site.ProtocolIdentification);
                    try
                    {
                        request.Identification = new IdentificationInfo()
                        {
                            Username = js["username"].ToString(), Password = js["password"].ToString()
                        };
                        request.IdentifyType = (LoginType)Enum.Parse(typeof(LoginType), js["type"].ToString());
                    }
                    catch (NullReferenceException)
                    {
                        request.Identification = new IdentificationInfo()
                        {
                            Password = null
                        };
                        request.IdentifyType = LoginType.Anonymous;
                    }

                    // 开始请求
                    bool result = await request.MakeRequest();

                    // 处理请求记录
                    CreateLogWithRequestServerResult(log, request);
                    // 补充额外添加的判断
                    log.Log_Record = request.ProtocalInfo;
                }
                // 更新站点信息
                UpdateSiteStatus(site, log);
                return(log);
            }
            return(null);
        }
Пример #6
0
        static void UploadToFTP(string FileToUpload)
        {
            FileInfo      FileInformation = new FileInfo(FileToUpload);
            string        FTPUri          = "ftp://localhost/" + FileInformation.Name;
            FtpWebRequest FTPRequest;

            //Create FtpWebRequest object and passing Creditails
            FTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://localhost/" + FileInformation.Name));
            //FTPRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            FTPRequest.Credentials = new NetworkCredential();

            //Close request after command is executed
            FTPRequest.KeepAlive = false;

            //Specify the command to be executed and mode
            FTPRequest.Method    = WebRequestMethods.Ftp.UploadFile;
            FTPRequest.UseBinary = true;

            //File size notification
            FTPRequest.ContentLength = FileInformation.Length;

            int buffLength = 2048; //2kb

            byte[] buff = new byte[buffLength];
            int    contentLen;

            //Open the file stream
            FileStream FTPFs = FileInformation.OpenRead();

            try
            {
                // Stream to which the file to be upload is written
                Stream StreamRequest = FTPRequest.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = FTPFs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the
                    // FTP Upload Stream
                    StreamRequest.Write(buff, 0, contentLen);
                    contentLen = FTPFs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                StreamRequest.Close();
                FTPFs.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press any key to contine...");
                Console.Read();
            }
        }
Пример #7
0
        /// <summary>
        /// 请求FTP服务器的状态
        /// </summary>
        /// <param name="site">待请求的站点</param>
        /// <param name="request">请求对象</param>
        public async Task AccessFTPServer(SiteModel site, FTPRequest request)
        {
            if (null != site.Site_address && !("".Equals(site.Site_address)))
            {
                // 检测并赋值DNS服务器IP
                IPAddress ip = await GetIPAddressAsync(site.Site_address);

                if (null == ip)
                {
                    try
                    {
                        // 获取请求站点的地址
                        ip = IPAddress.Parse(site.Site_address);
                    }
                    catch (ArgumentException e)
                    {
                        Debug.WriteLine(e.ToString());
                        DBHelper.InsertErrorLog(e);
                    }
                }
                request.FtpServer = IPAddress.Parse(site.Site_address);
                // 这里待定!!!!!因为用于保存用户身份识别信息的变量未知!!
                request.Identification = new IdentificationInfo()
                {
                    Username = site.ProtocolIdentification, Password = site.ProtocolIdentification
                };
                #region 初始化log
                LogModel log = new LogModel
                {
                    Site_id     = site.Id,
                    Create_time = DateTime.Now
                };
                #endregion
                // 开始请求
                bool result = await request.MakeRequest();

                // 处理请求记录
                CreateLogWithRequestServerResult(log, request);
                // 补充额外添加的判断
                log.Log_record = request.ProtocalInfo;
                // 更新站点信息
                UpdateSiteStatus(site, log);
            }
        }
        public void TestAccessFTPServer()
        {
            FTPRequest request = FTPRequest.Instance;

            request.Identification = new IdentificationInfo()
            {
                Username = "******", Password = "******"
            };
            SiteModel site = new SiteModel()
            {
                Site_address = "47.94.251.85", ProtocolIdentification = ""
            };

            utilObject.AccessFTPServer(site, request);

            // 判断这次请求是否发生
            Assert.IsFalse(string.IsNullOrEmpty(request.ProtocalInfo));
            // 判断这次预计成功的请求是否成功
            Assert.AreEqual(site.Status_code, "1000");
            // 判断站点请求次数是否符合请求逻辑
            Assert.AreEqual(site.Request_count, 1);
        }
Пример #9
0
        /// <summary>
        /// 请求FTP服务器的状态
        /// </summary>
        /// <param name="site">待请求的站点</param>
        /// <param name="request">请求对象</param>
        public async Task <LogModel> AccessFTPServer(SiteModel site, FTPRequest request)
        {
            if (null != site.Site_address && !("".Equals(site.Site_address)))
            {
                // 检测并赋值DNS服务器IP
                IPAddress ip = await GetIPAddressAsync(site.Site_address);

                // IP 不合法
                if (null == ip)
                {
                    return(null);
                }
                request.FtpServer = IPAddress.Parse(site.Site_address);
                // 获取保存的用户验证的信息
                JObject js = (JObject)JsonConvert.DeserializeObject(site.ProtocolIdentification);
                request.Identification = new IdentificationInfo()
                {
                    Username = js["useaname"].ToString(), Password = js["password"].ToString()
                };
                #region 初始化log
                LogModel log = new LogModel
                {
                    Site_id     = site.Id,
                    Create_time = DateTime.Now
                };
                #endregion
                // 开始请求
                bool result = await request.MakeRequest();

                // 处理请求记录
                CreateLogWithRequestServerResult(log, request);
                // 补充额外添加的判断
                log.Log_record = request.ProtocalInfo;
                // 更新站点信息
                UpdateSiteStatus(site, log);
                return(log);
            }
            return(null);
        }
 public FTPCreateFolderRequest(string subDir, FTPRequest request) : base(request)
 {
     this.SubDir = subDir;
 }
 public FTPFileUploadRequest(string filePath, string baseFolder, FTPRequest uploadRequest)
     : base(uploadRequest)
 {
     this.FilePath   = filePath;
     this.BaseFolder = baseFolder;
 }
Пример #12
0
        private async void BackGroundRequestTask(IBackgroundTaskInstance taskInstance)
        {
            MessageRemind toast    = new MessageRemind(); //初始化消息提醒
            var           sitelist = DBHelper.GetAllSite();
            var           len      = sitelist.Count;      //使用foreach会出现不在期望中的异常
            SiteModel     _presite = new SiteModel();

            _presite = DBHelper.GetSiteById(4);            //这里是指定了precheck的id为4
            var _precolor = _presite.Last_request_result;  //如果percheck为错误 就不进行请求了

            if (_precolor != 0)
            {
                //遍历sitelist 根据协议进行请求
                for (int i = 0; i < len; i++)
                {
                    SiteModel si          = sitelist[i];
                    string    _protocol   = si.Protocol_type;
                    string    _address    = si.Site_address;
                    string    url         = _address;
                    bool      _is_Monitor = si.Is_Monitor;
                    if (!_is_Monitor)
                    {
                        continue;
                    }
                    if (!IPAddress.TryParse(url, out IPAddress reIP))
                    {
                        //如果输入的不是ip地址
                        //通过域名解析ip地址
                        //网址简单处理 去除http和https
                        var http  = url.StartsWith("http://");
                        var https = url.StartsWith("https://");
                        if (http)
                        {
                            url = url.Substring(7);//网址截取从以第一w
                        }
                        else if (https)
                        {
                            url = url.Substring(8);//网址截取从以第一w
                        }
                        IPAddress[] hostEntry = await Dns.GetHostAddressesAsync(url);

                        for (int m = 0; m < hostEntry.Length; m++)
                        {
                            if (hostEntry[m].AddressFamily == AddressFamily.InterNetwork)
                            {
                                reIP = hostEntry[m];
                                break;
                            }
                        }
                    }//根据地址解析出ipv4地址
                    switch (_protocol)//根据协议请求站点
                    {
                    case "HTTPS":
                        HTTPRequest hTTPs = HTTPRequest.Instance;
                        hTTPs.ProtocolType = TransportProtocol.https;    //更改协议类型
                        hTTPs.Uri          = _address;
                        bool httpsFlag = await hTTPs.MakeRequest();

                        //请求完毕
                        //处理数据
                        si.Request_interval = hTTPs.TimeCost;
                        si.Request_count   += 1;
                        if ("1002".Equals(hTTPs.Status))    //定义的超时状态码
                        {
                            //请求超时
                            si.Last_request_result = -1;
                        }
                        else
                        {
                            SiteDetailViewModel util = new SiteDetailViewModel(); //用于查看状态码
                            bool match = util.SuccessCodeMatch(si, hTTPs.Status); //匹配用户设定状态码
                            if (match)                                            //匹配为成功  否则为失败
                            {
                                si.Last_request_result = 1;
                            }
                            else
                            {
                                si.Last_request_result = 0;
                                toast.ShowToast(si);
                            }
                        }
                        break;

                    case "HTTP":
                        HTTPRequest hTTP = HTTPRequest.Instance;    //默认协议类型http
                        hTTP.Uri          = _address;
                        hTTP.ProtocolType = TransportProtocol.http;
                        bool httpFlag = await hTTP.MakeRequest();

                        //请求完毕
                        //处理数据
                        si.Request_interval = hTTP.TimeCost;
                        si.Request_count   += 1;
                        if ("1002".Equals(hTTP.Status))
                        {
                            //请求超时
                            si.Last_request_result = -1;
                        }
                        else
                        {
                            SiteDetailUtilImpl util = new SiteDetailUtilImpl();
                            bool match = util.SuccessCodeMatch(si, hTTP.Status);    //匹配用户设定状态码
                            if (match)
                            {
                                si.Last_request_result = 1;
                            }
                            else
                            {
                                si.Last_request_result = 0;
                            }
                        }
                        if (httpFlag == false)
                        {
                            toast.ShowToast(si);
                            si.Last_request_result = 0;
                        }
                        break;

                    case "DNS":
                        string     baiduDomain = "www.baidu.com";//设定一个网站供dns服务器进行解析
                        DNSRequest dNS         = new DNSRequest(reIP, baiduDomain);
                        bool       dnsFlag     = await dNS.MakeRequest();

                        //请求完毕
                        if ("1000".Equals(dNS.Status))
                        {
                            //dns正常
                            si.Last_request_result = 1;
                        }
                        else if ("1001".Equals(dNS.Status))
                        {
                            //unknown
                            si.Last_request_result = 2;
                        }
                        else if ("1002".Equals(dNS.Status))
                        {
                            //timeout
                            si.Last_request_result = -1;
                        }
                        si.Request_interval = dNS.TimeCost;
                        si.Request_count   += 1;
                        if (dnsFlag == false)
                        {
                            //消息提醒
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "ICMP":
                        ICMPRequest icmp     = new ICMPRequest(reIP);
                        bool        icmpFlag = icmp.DoRequest();
                        //请求完毕
                        RequestObj requestObj;    //用于存储icmp请求结果的对象
                        requestObj             = DataHelper.GetProperty(icmp);
                        si.Last_request_result = int.Parse(requestObj.Color);
                        si.Request_count      += 1;
                        si.Request_interval    = requestObj.TimeCost;
                        if (icmpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "FTP":
                        var     json = si.ProtocolIdentification;
                        JObject js   = (JObject)JsonConvert.DeserializeObject(json);
                        //在此处加入type类型
                        string     username = js["username"].ToString();
                        string     password = js["password"].ToString();
                        FTPRequest fTP      = new FTPRequest(LoginType.Identify);
                        fTP.FtpServer = reIP;
                        fTP.Identification.Username = username;
                        fTP.Identification.Password = password;
                        bool ftpFlag = await fTP.MakeRequest();

                        //请求完毕
                        if ("1001".Equals(fTP.Status))
                        {
                            //置为错误
                            si.Last_request_result = 0;
                        }
                        else if ("1000".Equals(fTP.Status))
                        {
                            //置为成功
                            si.Last_request_result = 1;
                        }
                        else if ("1002".Equals(fTP.Status))
                        {
                            //超时异常
                            si.Last_request_result = -1;
                        }
                        si.Request_count   += 1;
                        si.Request_interval = fTP.TimeCost;
                        if (ftpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "SMTP":
                        SMTPRequest sMTP     = new SMTPRequest(_address, si.Server_port);
                        bool        smtpFlag = await sMTP.MakeRequest();

                        //请求完毕

                        if ("1000".Equals(sMTP.Status))
                        {
                            si.Last_request_result = 1;
                        }
                        else if ("1001".Equals(sMTP.Status))
                        {
                            si.Last_request_result = 0;
                        }
                        else if ("1002".Equals(sMTP.Status))
                        {
                            si.Last_request_result = -1;
                        }
                        si.Request_count   += 1;
                        si.Request_interval = sMTP.TimeCost;
                        if (smtpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    default:
                        break;
                    }
                    DBHelper.UpdateSite(si);
                }
            }
        }