0

Sorry if my question was not in proper manner do edit if required.

USE CASE

I want a function which will find the given string or text from a web page as soon as it updated in wepage in c#.

Take example as https://www.worldometers.info/world-population/ i am trying to get "40" data.So,when the current page will load "40" data or content my function should stop.I think its not loading the AJAX calls.

public static void WaitForWebPageContent(string url,string text)
{
    while (true)
    {
        string pageContent = null;
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse myres = (HttpWebResponse)myReq.GetResponse();

        using (StreamReader sr = new StreamReader(myres.GetResponseStream()))
        {
            pageContent = sr.ReadToEnd();
        }

        if(pageContent.Contains(text))
        {
            Debug.WriteLine("Found it");
            break;
        }
    }
}

My Question

I am searching for a method where i can get the page content by calling it from my function.Currently i am using httpclient to get the response from a URL but the data its not stopping even the content is loaded in browser.I am continously sending the request in each and every second by using the above code.Please guide me to proper solution

Thanks

  • You need to get the outerhtml from the request. – jdweng Dec 19 '20 at 10:52
  • Can you explain how will i acheive that ? – Firoz Rangrez Dec 19 '20 at 14:17
  • The managed c# Net libraries block direct accessing of the html response. So you need to use an unmanged library to access the html. Most people use the htmlagilitypack which calls the microsoft com dll. See : https://stackoverflow.com/questions/16642196/get-html-code-from-website-in-c-sharp. You can use same by adding from menu Project : Add Reference : Browse : C:\windows\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll – jdweng Dec 19 '20 at 15:25

0 Answers0