Beispiel #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                var client = new FluentFTP.FtpClient("104.46.7.78", @"ftpuser2", "password");

                // Change this value to a string of the path you would like to check for your user.  My user name was AzrueUser
                var items = await client.GetListingAsync("/home/AzureUser");

                foreach (var i in items)
                {
                    log.LogInformation($"{i.Name} {i.Size}");
                }
            }
            catch (Exception exc)
            {
                log.LogInformation(exc.Message);
            }


            string name = req.Query["name"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return(new OkObjectResult(responseMessage));
        }
Beispiel #2
0
        /// <summary>
        /// 导航到指定的地址。需要登录时自动弹出登录界面。导航失败时,自动导航到失败页面。
        /// 不抛出异常。
        /// </summary>
        /// <param name="address">要导航到的地址</param>
        /// <returns>导航是否成功</returns>
        private async Task <bool> NavigateAsync(Uri address)
        {
            await ftpSemaphore.WaitAsync();

            try
            {
                errorMessage.Visibility     = Visibility.Collapsed;
                progressBar.Visibility      = Visibility.Visible;
                progressBar.IsIndeterminate = true;

                currentAddress = address;
                // 显示新的地址
                UriBuilder uriBuilder = new UriBuilder(currentAddress);
                uriBuilder.UserName = string.Empty;
                uriBuilder.Password = string.Empty;
                addressBox.Text     = uriBuilder.Uri.ToString();


                // 不是ftp协议则抛出异常
                if (address.Scheme != "ftp" && address.Scheme != "ftps")
                {
                    throw new InvalidOperationException("只支持ftp协议");
                }

                // Host改变时重新连接
                if (client == null)
                {
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }
                else if (client.Host != address.Host ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.None && address.Scheme == "ftp") ||
                         (client.EncryptionMode != FluentFTP.FtpEncryptionMode.Explicit && address.Scheme == "ftps") ||
                         (client.Port != 21 && address.Port < 0) ||
                         (client.Port != address.Port && address.Port >= 0) ||
                         !string.IsNullOrEmpty(address.UserInfo))
                {
                    client.Disconnect();
                    client.Dispose();
                    client = CreateFtpClient(address, GetCredential(address.UserInfo), address.Scheme == "ftps");
                    await FtpConnectAsync(client);
                }

                // FTP路径可能包含#号,#号后面的内容会被归入Fragment。
                string remotePath = address.LocalPath + address.Fragment;

                listItemsVM.Clear();
                foreach (var item in (await client.GetListingAsync(remotePath)).OrderBy(x => x.Name))
                {
                    listItemsVM.Add(await FtpListItemViewModel.FromFtpListItemAsync(item));
                }

                using (Data.HistoryContext db = new Data.HistoryContext())
                {
                    Data.HistoryEntry h = new Data.HistoryEntry
                    {
                        Time = DateTimeOffset.Now,
                        Url  = uriBuilder.Uri.ToString()
                    };
                    await db.AddAsync(h);

                    await db.SaveChangesAsync();
                }

                return(true);
            }
            catch (FluentFTP.FtpCommandException exception)
            {
                if (exception.CompletionCode == "530")
                {
                    errorMessage.Text = "认证失败,请尝试登录。";
                    loginButton.Flyout.ShowAt(loginButton);
                    loginErrorMessage.Visibility = Visibility.Visible;
                }
                else
                {
                    errorMessage.Text = string.Format(
                        "发生错误,FTP返回代码:{0}。详细信息:\n{1}", exception.CompletionCode, exception.Message);
                }

                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            catch (Exception exception)
            {
                errorMessage.Text       = string.Format("发生错误。详细信息:\n{0}", exception.Message);
                errorMessage.Visibility = Visibility.Visible;
                listItemsVM.Clear();

                return(false);
            }
            finally
            {
                progressBar.Visibility = Visibility.Collapsed;
                ftpSemaphore.Release();
            }
        }