Can we download the pages of the code without an announcement in XAMl?


  • QA Engineer

    Can we download the pages of the code without an announcement in XAMl?

    The code doesn't work. The event will never come.

    WebBrowser browser = new WebBrowser();
    browser.Navigate(new Uri("https://www.google.ru/"));
    browser.LoadCompleted += brows_DocumentCompleted;     
    

    If you announce WebBrowser in XAMl and use the code, it works.
    This is happening.

    browserXaml.Navigate(new Uri("https://www.google.ru/"));
    browserXaml.LoadCompleted += brows_DocumentCompleted;  
    


  • If you want to download the file without a visual component, don't use it.

    There are special classes for this. WebClient (more old) and HttpClient (more new).

    Example of use:

    string s;
    using (var cl = new WebClient())
       s = cl.DownloadString("https://www.google.ru/");
    

    or better asynchronous:

    string s;
    using (var cl = new WebClient())
       s = await cl.DownloadStringAsync("https://www.google.ru/");
    

    You can also https://msdn.microsoft.com/en-us/library/System.Net.WebClient%28v=vs.110%29.aspx .

    Same for HttpClient:

    string s;
    using (var cl = new HttpClient())
       s = await cl.GetStringAsync("https://www.google.ru/");
    

    (sighs)HttpClientseems to support only asynchronous operations. )

    If you want to read the flow as the answer comes, you can use it. GetStreamAsync:

    using (var cl = new HttpClient())
    using (var stream = await cl.GetStreamAsync("https://www.google.ru/"))
    using (var reader = new StreamReader(stream))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
            Console.WriteLine(line);
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2