예제 #1
0
        /// <summary>
        /// Opens the given url
        /// </summary>
        /// <param name="method"></param>
        /// <param name="url"></param>
        /// <param name="data"></param>
        private void Open(string method, string url, string data)
        {
            if (m_Window != null)
            {
                m_Window.FireOnUnload(m_Window, new EventArgs());
                m_Window = null;
            }

            url = AbsolizeUrl(url);

            m_Uri = new Uri(url);

            m_Window          = new HtmlWindow(this, null);
            m_Window.Document = new HtmlDocument(m_Window);

            if (m_Uri.Scheme == Uri.UriSchemeHttp ||
                m_Uri.Scheme == Uri.UriSchemeHttps)
            {
                WebRequest request = HttpWebRequest.CreateDefault(m_Uri);
                request.Method = method;
                if (data.Length > 0)
                {
                    request.ContentType   = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    foreach (WebCookie cookie in Cookies)
                    {
                        request.Headers.Add("Cookie", cookie.ToString());
                    }
                    using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) {
                        writer.Write(data);
                    }
                }

                WebResponse response = null;
                try {
                    response = request.GetResponse();
                } catch (WebException ex) {
                    response = ex.Response;
                }

                for (int i = 0; i < response.Headers.Count; i++)
                {
                    if (response.Headers.Keys[i] == "Set-Cookie")
                    {
                        Cookies.Add(response.Headers[i]);
                    }
                }

                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    new HtmlReader().Read(reader, m_Window.Document);
                }
            }
            else if (m_Uri.Scheme == Uri.UriSchemeFile)
            {
                using (StreamReader reader = new StreamReader(m_Uri.PathAndQuery)) {
                    new HtmlReader().Read(reader, m_Window.Document);
                }
            }


            m_Engine = new ScriptEngine(this);
            m_Engine.SetGlobalObject("navigator", this);
            m_Engine.SetGlobalObject("document", m_Window.Document);
            m_Engine.SetGlobalObject("window", m_Window);
            foreach (HtmlScriptElement ele in m_Window.Document.GetElementsByTagName("SCRIPT"))
            {
                m_Engine.AddCodeBlock(ele.SourceCodeId, ele.SourceCode);
            }


            m_Engine.Compile();
            m_Engine.Run();

            m_Window.FireOnLoad(m_Window, new EventArgs());
        }