0

I make http requests to a site but I want to get the source code of a page 5 seconds after the site loads.

    string html = string.Empty;
    string url = @"https://***.com";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36";
    request.CookieContainer = new CookieContainer(); 
    request.CookieContainer.Add(new Cookie("", "****") { Domain = "***" });                    
    request.AutomaticDecompression = DecompressionMethods.GZip;

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    html = reader.ReadToEnd();            
    ViewBag.Html = html; 
    return View();
  • 1
    `Thread.Sleep` ? – Koby Douek Apr 21 '17 at 12:56
  • Why would you want to do this? There is no difference between the response received from a HTTP request 5 seconds after or 0 seconds after. If you wanted to delay the response, this would be the responsibility of the server that is sending the data. – Luke Apr 21 '17 at 12:57
  • I think he wants to wait for some javascript to get executed on a page, which isn't really an easy task. 5 sec wait won't do it btw. – Andrei Apr 21 '17 at 13:00
  • The source code of the site Im trying to get information from changes 5 seconds after the site loads. – Ivaylo Nikolov Apr 21 '17 at 13:00
  • 1
    the source code, as his name can tell you, won't change. Source code is source code. If you want to check changes inside the DOM that JS will make, you will have to make the site run into a javascript engine. You can maybe have a look at chrome headless browser. – Deblaton Jean-Philippe Apr 21 '17 at 13:03
  • 2
    Possible duplicate of [Get the final generated html source using c# or vb.net](http://stackoverflow.com/questions/14847656/get-the-final-generated-html-source-using-c-sharp-or-vb-net) – Andrei Apr 21 '17 at 13:03
  • That means it changes some thing with JavaScript, I don't think you can achieve that with HttpWebRequest you may have to use WebBrowser for that – Krishna Apr 21 '17 at 13:04
  • Ye, I meant that the DOM changes. Sorry, my bad.I will try use WebBrowser or some javascript engine. I will reply again if i succeed. – Ivaylo Nikolov Apr 21 '17 at 13:04
  • I guess what I want to do is not going to happen. WebBrowser runs in a single threaded apartment. ASP.Net runs in multiple thread apartment. And as far as I know we cant change it. I tried to put STAThread attribute and it didnt work. – Ivaylo Nikolov Apr 21 '17 at 16:02

0 Answers0