Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am using below code to grab website title and I also want later to grab the website photo(s) just like what Facebook, linkedin and twitter is doing

The problem I facing here that it is too slow and takes very long time to show the title unlike when you do it using facebook or linkedin for example

How can I fix that please?


Thanks,
Jassim

What I have tried:

private async void Button_Clicked(object sender, EventArgs e)
{
    LabelWebsiteURL.Text = EntryURL.Text;

    HttpClient hc = new HttpClient();
    HttpResponseMessage response = await hc.GetAsync(new Uri(LabelWebsiteURL.Text, UriKind.Absolute));
    string source = await response.Content.ReadAsStringAsync();

    string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

    LabelWebsiteTitle.Text = title;
}
Posted
Updated 19-Feb-20 2:05am
Comments
Richard MacCutchan 16-Feb-20 9:21am    
As I told you earlier, your code has to wait for the remote site to send a response and then read all the text for the Regex to find what you are looking for. You cannot increase the speed of remote sites through coding.
[no name] 16-Feb-20 10:07am    
"You cannot increase the speed of remote sites through coding": +5
Richard MacCutchan 16-Feb-20 10:51am    
Ha ha, but what do I know?
[no name] 16-Feb-20 11:13am    
Ever bothered to check the length of data returned?

1 solution

As Richard said, you can't do anything in your code to make the remote site return a response any faster.

However, you can potentially shave a small amount of time and memory off by streaming the response, and only reading up to the closing </head> tag.

For example:
C#
public static class HttpContentExtensions
{
    public static async Task<string> ExtractHtmlHeadAsync(this HttpContent content)
    {
        if (content is null) throw new ArgumentNullException(nameof(content));
        
        using (var stream = await content.ReadAsStreamAsync())
        using (var reader = new StreamReader(stream))
        {
            int charsRead;
            char[] buffer = new char[1024];
            var sb = new StringBuilder();
            
            while ((charsRead = await reader.ReadBlockAsync(buffer, 0, buffer.Length)) != 0)
            {
                sb.Append(buffer, 0, charsRead);
                
                int index = FindClosingHeadIndex(sb);
                if (index != -1)
                {
                    sb.Length = index + 7;
                    break;
                }
            }
            
            return sb.ToString();
        }
    }
    
    private static int FindClosingHeadIndex(StringBuilder sb)
    {
        for (int index = 0; index < sb.Length - 6; index++)
        {
            if (sb[index] == '<'
                && sb[index + 1] == '/'
                && sb[index + 6] == '>'
                && (sb[index + 2] == 'h' || sb[index + 2] == 'H')
                && (sb[index + 3] == 'e' || sb[index + 3] == 'E')
                && (sb[index + 4] == 'a' || sb[index + 4] == 'A')
                && (sb[index + 5] == 'd' || sb[index + 2] == 'D'))
            {
                return index;
            }
        }
        
        return -1;
    }
}
C#
private void Button_Clicked(object sender, EventArgs e)
{
    if (!Uri.TryCreate(EntryURL.Text, UriKind.Absolute, out var url))
    {
        MessageBox.Show("Please enter a valid URL!");
        return;
    }
    
    _ = LoadWebsiteTitleAsync(url);
}

private async Task LoadWebsiteTitleAsync(Uri url)
{
    LabelWebsiteURL.Text = url.ToString();
    
    var hc = new HttpClient();
    
    using (var response = await hc.GetAsync(url, HttpCompletionOption.ResponseContentRead))
    {
        string source = await response.Content.ExtractHtmlHeadAsync();
        Match match = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\s*\</title\>", RegexOptions.IgnoreCase);
        string title = match.Success ? match.Groups["Title"].Value : "# Untitled page #";
        LabelWebsiteTitle.Text = title;
    }
}
NB: Avoid async void methods | You’ve Been Haacked[^]
Async Guidance - David Fowler[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900