0

I'm trying to load a webpage in my application background. following code shows How I am loading a page:

            request = (HttpWebRequest)WebRequest.Create("http://example.com");
            request.CookieContainer = cookieContainer;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream st = response.GetResponseStream();
                StreamReader sr = new StreamReader(st);
                string responseString = sr.ReadToEnd();
                sr.Close();
                st.Close();
            }  

as you know, the server responses HTML codes or some javascript codes, but there are many codes which added to the webpage by javascripts functions. so I have to interpret or compile the first HTTP response.
I tried to use System.Windows.Forms.WebBrowser object to load the webpage completely, but this is a weak engine to do this.
so I tried to use CEFSharp (Chromium embedded Browser), it's great and works fine but I have trouble with that. following is how I use CEFSharp to load a webpage:

ChromiumWebBrowser MainBrowser = new ChromiumWebBrowser("http://Example/");  
MainBrowser.FrameLoadEnd+=MainBrowser.FrameLoadEnd;
panel1.Controls.Add(MainBrowser);
MainBrowser.LoadHtml(responseString,"http://example.com");

it works fine when I use this code in Form1.cs and when I add MainBrowser to a panel. but I want to use it in another class, actually ChromiumWebBrowser is part of another custom object and the custom object works in background. also it would possible 10 or 20 custom objects work in a same time. in this situation ChromiumWebBrowser doesn't work any more!

second problem is the threading issue, when I call this function MainBrowser.LoadHtml(responseString,"http://example.com"); it doesn't return any results, so I have to pause the code execution by using Semaphore and wait for the result at this event: MainBrowser.FrameLoadEnd

so I wish my code be some thing like this:

request = (HttpWebRequest)WebRequest.Create("http://example.com");
            request.CookieContainer = cookieContainer;
            string responseString="";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Stream st = response.GetResponseStream();
                StreamReader sr = new StreamReader(st);
                responseString = sr.ReadToEnd();
                sr.Close();
                st.Close();
            } 
            string FullPageContent = SomeBrowserEngine.LoadHtml(responseString);
            //Do stuffs

Can you please show me how to do this? do you know any other web browser engines that work like what I want?
please tell me if I'm doing any things wrong with CEFSharp or other concepts.

m.Khaki
  • 73
  • 3
  • 13
  • 1
    http://awesomium.com/ – Alex K. Jul 22 '16 at 15:57
  • Do you need Chrome specific features (i.e. WebGL)? Basic IE11 available via WebBrowser (http://stackoverflow.com/questions/17922308/use-latest-version-of-ie-in-webbrowser-control) may be enough. – Alexei Levenkov Jul 22 '16 at 16:31
  • Browser has async interface, but browser doesn't produce random requests. So after browser created, you just need to wait until it load ends (for specific frame), or for all frames, or check for browser loading state. Depending on your's needs it is can be acceptable solution or not. – Dmitry Azaraev Jul 24 '16 at 16:35
  • You doesn't store/load cookies into browser. Many pages will be completely broken without them. Also CefSharp's LoadHtml method uses internal resource handler machinery, which IMHO will not work with pages who self-redirect (because any following request for same url will return original text, instead of network-based). If it is true, then it is looks like bug / should be reported to CefSharp. Basically CEF is the best thing which you can choose. – Dmitry Azaraev Jul 24 '16 at 16:41

0 Answers0