【发布时间】:2020-09-07 19:50:13
【问题描述】:
我不确定从哪里开始,我从模板管理代码。使用下面的代码,我可以从 Http 服务器端下载所有文件。它会检查这是否已经下载,如果是,那么它不会从站点中获取它。我只想下载部分文件。我正在尝试想一个简单的解决方案来实现以下几点:
- 在 Server-Http 上获取上次修改数据或上次创建时间。我了解如何从文件夹执行此操作,但我不想下载文件然后检查,我需要在服务器上执行此操作。 Onlocal pc 将是
FileInfo infoSource = new FileInfo(sourceDir);,然后是infoSource.CreationTime,其中 sourceDir 是文件路径。 http 上可能有类似的东西吗? - 仅从服务器站点获取最新的 10 个文件。不是最新的,而是最新的 10 个。
- 监控服务器站点,一旦有一个文件 MyFileName_Version 放在站点上,它将获取具有此命名约定的最新文件。
这些方法中的任何一种都对我有用,但我仍然是这些方法的新手,所以在这里努力。 目前我有以下代码:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using Topshelf;
namespace AutomaticUpgrades
{
class Program
{
static void Main(string[] args)
{
// This path containts of the Site, Then binary-release/, The
string url = "HTTP://LOCALHOUST:1000000";
DownloadDataFromArtifactory(url);
}
private static void DownloadDataFromArtifactory(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
MatchCollection matches = regex.Matches(html);
if (matches.Count > 0)
{
WebClient webClient = new WebClient();
foreach (Match match in matches)
{
if (match.Success)
{
Console.WriteLine(match.Groups["name"]);
//"C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download"
if (match.Groups["name"].Length > 5
&& DupeFile(match.Groups["name"].ToString(),
"C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download")
)
{
webClient.DownloadFile("HTTP://LOCALHOUST:1000000" + match.Groups["name"], "C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download\\" + match.Groups["name"]);
}
}
}
webClient.Dispose();
}
}
}
}
public static string GetDirectoryListingRegexForUrl(string url)
{
if (url.Equals("HTTP://LOCALHOUST:1000000"))
{
return "<a href=\".*\">(?<name>.*)</a>";
}
throw new NotSupportedException();
}
private static bool DupeFile(string httpFile, string folderLocation)
{
string[] files = System.IO.Directory.GetFiles(folderLocation);
foreach (string s in files)
{
if (System.IO.Path.GetFileName(s).ToString() == httpFile)
{
return false;
}
}
return true;
}
}
}
【问题讨论】:
-
使用 FTP 下载文件名,并在下载前与现有文件进行比较。请参阅:docs.microsoft.com/en-us/dotnet/framework/network-programming/…
-
如果我知道 JFrog 用户名和密码,为什么它仍然会显示我“无法连接到远程服务器”
request.Credentials = new NetworkCredential("Username", "Password"); FtpWebResponse response = (FtpWebResponse)request.GetResponse();我假设 Http: 需要是正确的改成 FTP 了吗? -
FTP 实际上是一种 HTTP。要获取目录,请使用 FTP。您可以使用 FTP 或 HTTP 下载文件。 Windows 可以阻止用户名/密码,并要求密码在网络密码服务器上进行身份验证。
标签: c# httpserver file-monitoring