Windows Form应用程序在Web上找到链接 [英] Windows Form app find Link on Web
问题描述
我需要创建一种方法来找到网站(Hudson服务器)上应用程序的最新版本,并允许其下载.
I need to create a method that find the newest version of application on a website (Hudson server) and allow to download it.
现在,我使用正则表达式扫描所有HTML,找到href标签并搜索所需的字符串.
till now I use regex to scan all the HTML and find the href tags and search for the string I wish to.
我想知道是否有最简单的方法. 我附上了我今天使用的代码:
I want to know if there is a simplest way to do so. I attached the code I use today:
namespace SDKGui
{
public struct LinkItem
{
public string Href;
public string Text;
public override string ToString()
{
return Href;
}
}
static class LinkFinder
{
public static string Find(string file)
{
string t=null;
List<LinkItem> list = new List<LinkItem>();
// 1.
// Find all matches in file.
MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)",
RegexOptions.Singleline);
// 2.
// Loop over each match.
foreach (Match m in m1)
{
string value = m.Groups[1].Value;
LinkItem i = new LinkItem();
// 3.
// Get href attribute.
Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
RegexOptions.Singleline);
if (m2.Success)
{
i.Href = m2.Groups[1].Value;
}
// 4.
// Remove inner tags from text.
t = Regex.Replace(value, @"\s*<.*?>\s*", "",
RegexOptions.Singleline);
if (t.Contains("hms_sdk_tool_"))
{
i.Text = t;
list.Add(i);
break;
}
}
return t;
}
}
}
推荐答案
It is easy to collect all href
values and filter against any of your conditions using HtmlAgilityPack. The following method shows how to access a page, get all <a>
tags, and return a list of all href
values containing hms_sdk_tool_
:
private List<string> HtmlAgilityCollectHrefs(string url)
{
var webGet = new HtmlAgilityPack.HtmlWeb();
var doc = webGet.Load(url);
var a_nodes = doc.DocumentNode.SelectNodes("//a");
return a_nodes.Select(p => p.GetAttributeValue("href", "")).Where(n => n.Contains("hms_sdk_tool_")).ToList();
}
或者,如果您对1个返回字符串感兴趣,请使用
Or, if you are interested in 1 return string, use
private string GetLink(string url)
{
var webGet = new HtmlAgilityPack.HtmlWeb();
var doc = webGet.Load(url);
var a_nodes = doc.DocumentNode.SelectNodes("//a");
return a_nodes.Select(p => p.GetAttributeValue("href", "")).Where(n => n.Contains("hms_sdk_tool_")).FirstOrDefault();
}
这篇关于Windows Form应用程序在Web上找到链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!