Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,856 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi
I'm trying to get meta data information from website.
I'm using this code
But it gives a null or empty value.
Here is my code:
using HtmlAgilityPack;
using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
GetMetaDataFromUrl(textBox1.Text);
MetaInformation info = new MetaInformation(textBox1.Text);
label1.Text = info.Title;
label2.Text = info.Description;
label3.Text = info.SiteName;
var request = WebRequest.Create(info.ImageUrl);
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
pictureBox1.Image = Bitmap.FromStream(stream);
}
}
public static MetaInformation GetMetaDataFromUrl(string url)
{
// Get the URL specified
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var metaTags = document.DocumentNode.SelectNodes("//meta");
MetaInformation metaInfo = new MetaInformation(url);
if (metaTags != null)
{
int matchCount = 0;
foreach (var tag in metaTags)
{
var tagName = tag.Attributes["name"];
var tagContent = tag.Attributes["content"];
var tagProperty = tag.Attributes["property"];
if (tagName != null && tagContent != null)
{
switch (tagName.Value.ToLower())
{
case "title":
metaInfo.Title = tagContent.Value;
matchCount++;
break;
case "description":
metaInfo.Description = tagContent.Value;
matchCount++;
break;
case "twitter:title":
metaInfo.Title = string.IsNullOrEmpty(metaInfo.Title) ? tagContent.Value : metaInfo.Title;
matchCount++;
break;
case "twitter:description":
metaInfo.Description = string.IsNullOrEmpty(metaInfo.Description) ? tagContent.Value : metaInfo.Description;
matchCount++;
break;
case "keywords":
metaInfo.Keywords = tagContent.Value;
matchCount++;
break;
case "twitter:image":
metaInfo.ImageUrl = string.IsNullOrEmpty(metaInfo.ImageUrl) ? tagContent.Value : metaInfo.ImageUrl;
matchCount++;
break;
}
}
else if (tagProperty != null && tagContent != null)
{
switch (tagProperty.Value.ToLower())
{
case "og:title":
metaInfo.Title = string.IsNullOrEmpty(metaInfo.Title) ? tagContent.Value : metaInfo.Title;
matchCount++;
break;
case "og:description":
metaInfo.Description = string.IsNullOrEmpty(metaInfo.Description) ? tagContent.Value : metaInfo.Description;
matchCount++;
break;
case "og:image":
metaInfo.ImageUrl = string.IsNullOrEmpty(metaInfo.ImageUrl) ? tagContent.Value : metaInfo.ImageUrl;
matchCount++;
break;
}
}
}
metaInfo.HasData = matchCount > 0;
}
return metaInfo;
}
}
}
How can I make this code work?
Try replacing
GetMetaDataFromUrl(textBox1.Text);
MetaInformation info = new MetaInformation(textBox1.Text);
with
MetaInformation info = GetMetaDataFromUrl(textBox1.Text);