在下面的文章中,我跟着例子來創建web服務器目錄我的HttpRequest和列表文件: C# HttpWebRequest command to get directory listing的HttpWebRequest命令來獲取目錄列表
我試圖使用的例子有,從我的Web服務器列表中的文件。我可以列出鏈接中引用的示例服務器中的文件,但我的服務器只顯示最後添加的文件。我的代碼就像那裏的例子。 我注意到我的html代碼有點不同。有人有一個想法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>186.215.156.154 - /download/Zatix/Zatix - Satisfação Geral/</title>
</head>
<body>
<h1>
186.215.156.154 - /download/Zatix/Zatix - Satisfação Geral/</h1>
<hr>
<pre>
<a href="/download/Zatix/">[Para a pasta superior]</a>
<br>
<br>
sexta-feira, 19 de novembro de 2010 11:17 52355 <a href="/download/Zatix/Zatix%20-%20Satisfa%C3%A7%C3%A3o%20Geral/Zatix%20-%20Satisfa%C3%A7%C3%A3o%20Geral_3_00.zip">Zatix - Satisfação Geral_3_00.zip</a><br>sexta-feira, 19 de novembro de 2010 11:17 52355 <a href="/download/Zatix/Zatix%20-%20Satisfa%C3%A7%C3%A3o%20Geral/Zatix%20-%20Satisfa%C3%A7%C3%A3o%20Geral_4_00.zip">Zatix - Satisfação Geral_4_00.zip</a>
<br>
</pre>
<hr>
</body>
</html
我想我必須要改變的東西在GetDirectoryListingRegexForUrl方法的返回。
我的代碼是這樣的:
private string GetDirectoryListingRegexForUrl(string url)
{
if (url.Equals(Url));
{
return "<A HREF=\".*\">(?<name>.*)</A>";
}
throw new NotSupportedException();
}
public void ListStudies()
{
Url = BaseUrl + this.clientName + "/" + this.activeStudy + "/";
Console.WriteLine(Url);
CookieContainer cookies;
HttpWebResponse response;
HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(Url);
req.Credentials = _NetworkCredential;
req.CookieContainer = new CookieContainer();
req.AllowAutoRedirect = true;
cookies = req.CookieContainer;
try
{
response = (HttpWebResponse)req.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
Console.WriteLine("URL NÃO RESPONDEU");
else
Console.WriteLine("URL OK");
using (response)
{
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)
{
foreach (Match match in matches)
{
if (match.Success)
{
Console.WriteLine(match.Groups["name"]);
}
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我希望你能幫幫我!謝謝。
非常感謝,我使用的是早期的正則表達式浪費了大量的時間,它總是返回1場。由你給出的更新的正則表達式,工作真棒。 – 2013-10-16 09:15:22