C#.NET Web API 2, How can I extract a certain text from a website using the HttpContent on a HTTPGET async method?

223 views Asked by At

I am new to Web and part of my Web API 2 training is to create an API controller that uses an HttpClient and an HttpContent to read entire website and from that to return only a specific portion to a text.

When I call that website, it comes back to me as an object. To me this is considered to be the “public API” to use.

From that I am prompt to return only a specific portion into a text to the user.

Now, I have spent hours googling and trying to implement it using Regex expressions, Newtonsoft.Json objects…etc, but I cannot seem to break that down.

Is there a way to accomplice that? What am I missing? Please advice!

 public async System.Threading.Tasks.Task<string> GetAsync()
        {
            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync("https://www.lipsum.com/"))
                {
                    using (HttpContent content = response.Content)
                    {
                        string text = await content.ReadAsStringAsync();
                        return text;
                    }
                }
            }
        }

Extract and store only this piece of text

1

There are 1 answers

0
Sotiris On

What I needed was an HTML parser. For that I have used AngleSharp.

I want to thank @Panagiotis Kanavos for his clarification and feedback.