Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:09:47+00:00 2026-06-08T14:09:47+00:00

I’m looking for a method that will allow me to get the title of

  • 0

I’m looking for a method that will allow me to get the title of a webpage and store it as a string.

However all the solutions I have found so far involve downloading the source code for the page, which isn’t really practical for a large number of webpages.

The only way I could see would be to limit the length of the string or it only downloads either a set number of chars or stops once it reaches the tag, however this obviously will still be quite large?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:09:48+00:00Added an answer on June 8, 2026 at 2:09 pm

    As the <title> tag is in the HTML itself, there will be no way to not download the file to find “just the title”. You should be able download a portion of the file until you’ve read in the <title> tag, or the </head> tag and then stop, but you’ll still need to download (at least a portion of) the file.

    This can be accomplished with HttpWebRequest/HttpWebResponse and reading in data from the response stream until we’ve either read in a <title></title> block, or the </head> tag. I added the </head> tag check because, in valid HTML, the title block must appear within the head block – so, with this check we will never parse the entire file in any case (unless there is no head block, of course).

    The following should be able to accomplish this task:

    string title = "";
    try {
        HttpWebRequest request = (HttpWebRequest.Create(url) as HttpWebRequest);
        HttpWebResponse response = (request.GetResponse() as HttpWebResponse);
    
        using (Stream stream = response.GetResponseStream()) {
            // compiled regex to check for <title></title> block
            Regex titleCheck = new Regex(@"<title>\s*(.+?)\s*</title>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            int bytesToRead = 8092;
            byte[] buffer = new byte[bytesToRead];
            string contents = "";
            int length = 0;
            while ((length = stream.Read(buffer, 0, bytesToRead)) > 0) {
                // convert the byte-array to a string and add it to the rest of the
                // contents that have been downloaded so far
                contents += Encoding.UTF8.GetString(buffer, 0, length);
    
                Match m = titleCheck.Match(contents);
                if (m.Success) {
                    // we found a <title></title> match =]
                    title = m.Groups[1].Value.ToString();
                    break;
                } else if (contents.Contains("</head>")) {
                    // reached end of head-block; no title found =[
                    break;
                }
            }
        }
    } catch (Exception e) {
        Console.WriteLine(e);
    }
    

    UPDATE: Updated the original source-example to use a compiled Regex and a using statement for the Stream for better efficiency and maintainability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.