如何用网页客户端获取网页的标题?
How to get title of web page with web client?
我的网络客户端连接有问题。在大多数网站上获取页面标题没有问题,但是当我测试它时发现一个网站引发错误 "The underlying connection was closed: An unexpected error occurred on a receive." 具体网站是:“https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html”。
private string GetTitle(string link)
{
try
{
string html = webClient.DownloadString(link);
Regex reg = new Regex("<title>(.*)</title>");
MatchCollection m = reg.Matches(html);
if (m.Count > 0)
{
return m[0].Value.Replace("<title>", "").Replace("</title>", "");
}
else
return "";
}
catch (Exception e)
{
return labelError.Text = "You should insert correct URL\n";
}
}
如果您想下载 html 并测试正则表达式,您可以使用下面的代码片段。
/*usage:*/
//HttpGet(new Uri("https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html"));
public static string HttpGet(Uri uri) {
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using(HttpWebResponse response = (HttpWebResponse) request.GetResponse())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream)) {
var str = reader.ReadToEnd();
Regex reg = new Regex("<title>(.*)</title>");
MatchCollection m = reg.Matches(str);
if (m.Count > 0) {
return m[0].Value.Replace("<title>", "").Replace("</title>", "");
} else
return "";
}
}
我的网络客户端连接有问题。在大多数网站上获取页面标题没有问题,但是当我测试它时发现一个网站引发错误 "The underlying connection was closed: An unexpected error occurred on a receive." 具体网站是:“https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html”。
private string GetTitle(string link)
{
try
{
string html = webClient.DownloadString(link);
Regex reg = new Regex("<title>(.*)</title>");
MatchCollection m = reg.Matches(html);
if (m.Count > 0)
{
return m[0].Value.Replace("<title>", "").Replace("</title>", "");
}
else
return "";
}
catch (Exception e)
{
return labelError.Text = "You should insert correct URL\n";
}
}
如果您想下载 html 并测试正则表达式,您可以使用下面的代码片段。
/*usage:*/
//HttpGet(new Uri("https://www.modbee.com/opinion/letters-to-the-editor/article111712867.html"));
public static string HttpGet(Uri uri) {
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using(HttpWebResponse response = (HttpWebResponse) request.GetResponse())
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream)) {
var str = reader.ReadToEnd();
Regex reg = new Regex("<title>(.*)</title>");
MatchCollection m = reg.Matches(str);
if (m.Count > 0) {
return m[0].Value.Replace("<title>", "").Replace("</title>", "");
} else
return "";
}
}