/// <summary>
        /// Gets a XmlNodeList from the queried HTML.
        /// </summary>
        /// <param name="data"> The HTML content to query.</param>
        /// <param name="query"> The XPath Query.</param>
        /// <returns> A XmlNodeList.</returns>
        public XmlNodeList GetNodes(string data,string query)
        {
            HtmlParser parser = new HtmlParser();

            try
            {
                XmlDocument doc;

                if ( this.AllowDocumentCaching )
                {
                    if ( cache == null )
                    {
                        //Load the file.
                        doc = new XmlDocument();
                        doc.Load(new StringReader(data));
                        this.cache=doc;
                    }
                    else
                    {
                        doc = this.cache;
                    }
                }
                else
                {
                    //Load the file.
                    doc = new XmlDocument();
                    doc.Load(new StringReader(data));
                }

                if ( this.namespaceCache==null)
                {
                    // create prefix<->namespace mappings (if any)
                    XmlNamespaceManager  nsMgr = new XmlNamespaceManager(doc.NameTable);

                    // resolve namespaces before loading document
                    Hashtable values = HtmlParser.ResolveNamespaces(new XmlTextReader(new StringReader(data)));

                    foreach (DictionaryEntry de in values)
                    {
                        nsMgr.AddNamespace((string)de.Key,(string)de.Value);
                    }

                    this.namespaceCache = nsMgr;
                }

                //Query the document
                XmlNodeList nodes = doc.SelectNodes(query, this.namespaceCache);

                return nodes;

            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Begins a new asynchronous SOAP HTTP request. This function is not thread safe.
        /// </summary>
        /// <param name="context"> The HttpRequestResponseContext type.</param>
        public void ExecuteSoapHttpWebRequest(HttpRequestResponseContext context)
        {
            HtmlParser parser = new HtmlParser();
            ScriptingHttpState httpRequestState = new ScriptingHttpState();
            httpRequestState.HttpRequestResponseContext = context;

            string uriString = context.Request.Url;

            if ( context.DecodeUrl )
            {
                uriString = EncodeDecode.UrlDecode(uriString);
            }

            bool isException = false;
            // create webrequest
            try
            {
                //this.ValidateIPAddress(new Uri(uriString));
                httpRequestState.HttpRequest = (HttpWebRequest)System.Net.WebRequest.Create(uriString);

                // Set HttpWebRequestProperties
                SetHttpWebRequestProperties(httpRequestState.HttpRequest, context.Request.RequestHttpSettings);

                // Apply proxy settings
                if ( this.ProxySettings != null )
                {
                    SetProxy(httpRequestState.HttpRequest,this.ProxySettings);
                }

                // Save cookies
                httpRequestState.HttpRequest.CookieContainer = new CookieContainer();
                if ( context.Request.Cookies != null )
                {
                    httpRequestState.HttpRequest.CookieContainer.Add(context.Request.GetCookieCollection());
                }

                byte[] data=null;
                SoapHttpWebRequest soapWebRequest = (SoapHttpWebRequest)context.Request;
                if ( soapWebRequest.XmlEnvelope != null )
                {
                    data = Encoding.UTF8.GetBytes(soapWebRequest.XmlEnvelope.OuterXml);

                    // set properties
                    //httpRequestState.HttpRequest.KeepAlive = true;
                    //httpRequestState.HttpRequest.Timeout = 10000;
                    httpRequestState.HttpRequest.Method = "POST";
                    httpRequestState.HttpRequest.ContentType = context.Request.RequestHttpSettings.ContentType;
                    httpRequestState.HttpRequest.ContentLength = data.Length;

                    // get request stream
                    Stream stm = httpRequestState.HttpRequest.GetRequestStream();
                    stm.Write(data,0,data.Length);
                    stm.Flush();
                    stm.Close();
                }

                // Get Response
                IAsyncResult ar = httpRequestState.HttpRequest.BeginGetResponse(new AsyncCallback(SessionScriptingCallback),httpRequestState);

                // register a timeout
                ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, new WaitOrTimerCallback(BaseHttpForm.RequestTimeoutCallback), httpRequestState, this.GetTimeout(), true);

            }
            catch
            {
                isException = true;
                throw;
            }
            finally
            {
                if (isException)
                {
                    if (httpRequestState.HttpResponse != null)
                    {
                        httpRequestState.HttpResponse.Close();
                    }
                }
            }
        }
        /// <summary>
        /// Begins a new  HTTP Post request. This function is not thread safe.
        /// </summary>
        /// <param name="context"> The HttpRequestResponseContext type.</param>
        public ResponseBuffer ExecutePostWebRequestSync(HttpRequestResponseContext context)
        {
            ResponseBuffer result = null;
            HtmlParser parser = new HtmlParser();
            ArrayList values = parser.GetArrayList(context.Request.Form.WriteHtmlFormTag());

            ScriptingHttpState httpRequestState = new ScriptingHttpState();
            httpRequestState.HttpRequestResponseContext = context;

            string uriString = context.Request.Url;

            if (context.DecodeUrl)
            {
                uriString = EncodeDecode.UrlDecode(uriString);
            }

            bool isException = false;

            // create webrequest
            try
            {
                //this.ValidateIPAddress(new Uri(uriString));

                httpRequestState.HttpRequest = (HttpWebRequest)System.Net.WebRequest.Create(uriString);

                // Set HttpWebRequestProperties
                SetHttpWebRequestProperties(httpRequestState.HttpRequest, context.Request.RequestHttpSettings);

                // Apply proxy settings
                if (this.ProxySettings != null)
                {
                    SetProxy(httpRequestState.HttpRequest, this.ProxySettings);
                }

                //httpRequestState.httpRequest.Referer = postUri;

                // Continue headers
                //hwr.ContinueDelegate = getRedirectHeaders;

                // Save cookies
                httpRequestState.HttpRequest.CookieContainer = new CookieContainer();
                if (context.Request.Cookies != null)
                {
                    httpRequestState.HttpRequest.CookieContainer.Add(context.Request.GetCookieCollection());
                }

                byte[] data = null;
                if (values != null)
                {
                    PostWebRequest postWebRequest = (PostWebRequest)context.Request;
                    string postData = string.Empty;

                    if (postWebRequest.UsePostData)
                    {
                        postData = postWebRequest.PostData;
                        httpRequestState.HttpRequest.ContentType = context.Request.RequestHttpSettings.ContentType;
                    }
                    else
                    {
                        // transform to postdata and encode in bytes
                        postData = GetPostData(values);
                        httpRequestState.HttpRequest.ContentType = "application/x-www-form-urlencoded";
                    }

                    data = Encoding.UTF8.GetBytes(postData);

                    // set properties
                    //httpRequestState.HttpRequest.AllowWriteStreamBuffering = false;
                    httpRequestState.HttpRequest.KeepAlive = true;
                    //httpRequestState.HttpRequest.Timeout = 120000;
                    httpRequestState.HttpRequest.Method = "POST";
                    httpRequestState.HttpRequest.ContentLength = data.Length;

                    // get request stream
                    Stream stm = httpRequestState.HttpRequest.GetRequestStream();
                    stm.Write(data, 0, data.Length);
                    stm.Flush();
                    stm.Close();
                }

                result = CreateSyncResponse(httpRequestState);

                // Get Response
                // IAsyncResult ar = httpRequestState.HttpRequest.BeginGetResponse(new AsyncCallback(SessionScriptingCallback), httpRequestState);

                // register a timeout
                // ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, new WaitOrTimerCallback(BaseHttpForm.RequestTimeoutCallback), httpRequestState, this.GetTimeout(), true);

            }
            catch
            {
                isException = true;
                throw;
            }
            finally
            {
                if (isException)
                {
                    if (httpRequestState.HttpResponse != null)
                    {
                        httpRequestState.HttpResponse.Close();
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Begins a new asynchronous HTTP Post request. This function is not thread safe.
        /// </summary>
        /// <param name="context"> The HttpRequestResponseContext type.</param>
        public void ExecutePostWebRequestFileUpload(HttpRequestResponseContext context)
        {
            PostWebRequest postWebRequest = (PostWebRequest)context.Request;

            HtmlFormTag formTag = postWebRequest.Form.WriteHtmlFormTag();
            UploadFileInfo[] fileInfoItems = UploadFileInfo.GetUploadFiles(formTag);

            HtmlParser parser = new HtmlParser();
            ScriptingHttpState httpRequestState = new ScriptingHttpState();
            httpRequestState.HttpRequestResponseContext = context;

            string uriString = context.Request.Url;

            if ( context.DecodeUrl )
            {
                uriString = EncodeDecode.UrlDecode(uriString);
            }

            bool isException = false;
            // create webrequest
            try
            {
                httpRequestState.HttpRequest = (HttpWebRequest)System.Net.WebRequest.Create(uriString);

                // Set HttpWebRequestProperties
                SetHttpWebRequestProperties(httpRequestState.HttpRequest, context.Request.RequestHttpSettings);

                // Apply proxy settings
                if ( this.ProxySettings != null )
                {
                    SetProxy(httpRequestState.HttpRequest,this.ProxySettings);
                }

                // Save cookies
                httpRequestState.HttpRequest.CookieContainer = new CookieContainer();
                if ( context.Request.Cookies != null )
                {
                    httpRequestState.HttpRequest.CookieContainer.Add(context.Request.GetCookieCollection());
                }

                #region Post Data
                string boundary  = "--------------" + DateTime.Now.Ticks.ToString("x");

                byte[] data = null;
                // Build the trailing boundary string as a byte array
                // ensuring the boundary appears on a line by itself
                byte[] boundaryBytes = null;

                long sumFileLength = 0;
                foreach ( UploadFileInfo uploadFile in fileInfoItems )
                {
                    FileInfo fileInformation = new FileInfo(uploadFile.FileName);
                    sumFileLength += fileInformation.Length;
                }

                string postdata = GenerateMimeFormData(boundary, formTag, fileInfoItems);
                data = Encoding.UTF8.GetBytes(postdata);

                long length;

                if ( fileInfoItems.Length == 0 )
                {
                    boundaryBytes = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n\0");
                    length = data.Length + sumFileLength +
                        boundaryBytes.Length;
                }
                else
                {
                    boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                    length = data.Length + sumFileLength +
                        boundaryBytes.Length;
                }

                // set properties
                //httpRequestState.HttpRequest.AllowWriteStreamBuffering = false;
                httpRequestState.HttpRequest.ServicePoint.Expect100Continue = false;
                httpRequestState.HttpRequest.KeepAlive = true;
                httpRequestState.HttpRequest.Timeout = 120000;
                httpRequestState.HttpRequest.Method = "POST";
                httpRequestState.HttpRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                httpRequestState.HttpRequest.ContentLength = length;
                httpRequestState.HttpRequest.Headers.Add("Cache-Control","no-cache");

                // get request stream and write header
                Stream stm = httpRequestState.HttpRequest.GetRequestStream();
                stm.Write(data,0,data.Length);

                if ( fileInfoItems.Length > 0 )
                {
                    foreach ( UploadFileInfo uploadFile in fileInfoItems )
                    {
                        UploadFile(uploadFile, boundaryBytes, stm);
                    }
                }
                stm.Write(boundaryBytes, 0, boundaryBytes.Length);
                stm.Flush();
                stm.Close();
                #endregion

                // Get Response
                IAsyncResult ar = httpRequestState.HttpRequest.BeginGetResponse(new AsyncCallback(SessionScriptingCallback),httpRequestState);

                // register a timeout
                ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, new WaitOrTimerCallback(BaseHttpForm.RequestTimeoutCallback), httpRequestState, this.GetTimeout(), true);

            }
            catch
            {
                isException = true;
                throw;
            }
            finally
            {
                if (isException)
                {
                    if (httpRequestState.HttpResponse != null)
                    {
                        httpRequestState.HttpResponse.Close();
                    }
                }
            }
        }
        /// <summary>
        /// Begins a new asynchronous HTTP Post request. This function is not thread safe.
        /// </summary>
        /// <param name="uriString"> The URL string.</param>
        /// <param name="clientSettings"> The client settings.</param>
        /// <param name="form"> The HTML Form tag.</param>
        /// <param name="cookies"> The cookies collection.</param>
        /// <param name="decodeUrl"> Decodes the url in Url and Html decoding.</param>
        public void StartAsyncHttpPost(string uriString, HttpProperties clientSettings, HtmlFormTag form, CookieCollection cookies, bool decodeUrl)
        {
            HtmlParser parser = new HtmlParser();
            ArrayList values = parser.GetArrayList(form);

            httpRequestState = new HttpState();

            if ( decodeUrl )
            {
                uriString = EncodeDecode.UrlDecode(uriString);
                uriString = EncodeDecode.HtmlDecode(uriString);
            }

            // create webrequest
            try
            {
                //this.ValidateIPAddress(new Uri(uriString));

                httpRequestState.HttpRequest = (HttpWebRequest)WebRequest.Create(uriString);

                // Set HttpWebRequestProperties
                SetHttpWebRequestProperties(httpRequestState.HttpRequest, clientSettings);

                // Apply proxy settings
                if ( this.ProxySettings != null )
                {
                    SetProxy(httpRequestState.HttpRequest,this.ProxySettings);
                }

                //httpRequestState.httpRequest.Referer = postUri;

                // Continue headers
                //hwr.ContinueDelegate = getRedirectHeaders;

                // save cookies
                httpRequestState.HttpRequest.CookieContainer=new CookieContainer();

                if ( cookies!=null )
                {
                    httpRequestState.HttpRequest.CookieContainer.Add(cookies);
                }

                byte[] data=null;
                if (values!=null)
                {
                    // transform to postdata and encode in bytes
                    string postData = GetPostData(values);
                    data = Encoding.UTF8.GetBytes(postData);

                    // set properties
                    httpRequestState.HttpRequest.Method="POST";
                    httpRequestState.HttpRequest.ContentType="application/x-www-form-urlencoded";
                    httpRequestState.HttpRequest.ContentLength=data.Length;

                    // get request stream
                    Stream stm = httpRequestState.HttpRequest.GetRequestStream();
                    stm.Write(data,0,data.Length);
                    stm.Flush();
                    stm.Close();
                }

                // Get Response
                IAsyncResult ar = httpRequestState.HttpRequest.BeginGetResponse(new AsyncCallback(FastResponseCallback),httpRequestState);

                // register a timeout
                ThreadPool.RegisterWaitForSingleObject(ar.AsyncWaitHandle, new WaitOrTimerCallback(BaseHttpForm.RequestTimeoutCallback), httpRequestState, this.GetTimeout(), true);

            }
            catch
            {
                throw;
            }
            finally
            {
                if ( httpRequestState.HttpResponse != null )
                {
                    httpRequestState.HttpResponse.Close();
                }
            }
        }
        /// <summary>
        /// Generates a MIME multipart/form-data header.
        /// </summary>
        /// <param name="boundary"> The boundary.</param>
        /// <param name="formTag"> The HtmlFormTag value.</param>
        /// <param name="fileItems"> The UploadFileInfo items.</param>
        /// <returns> A MIME multipart/form-data header string.</returns>
        protected string GenerateMimeFormData(string boundary, Ecyware.GreenBlue.Engine.HtmlDom.HtmlFormTag formTag, UploadFileInfo[] fileItems)
        {
            // create hashtable for upload file infos.
            Hashtable files = new Hashtable();
            foreach ( UploadFileInfo fileInfo in fileItems )
            {
                files.Add(fileInfo.FormFieldName, fileInfo);
            }

            HtmlParser parser = new HtmlParser();
            ArrayList values = parser.GetArrayList(formTag);

            StringBuilder mimeData = new StringBuilder();

            // Create form fields
            foreach ( string nameValuePair in values )
            {
                string name = EncodeDecode.UrlDecode(nameValuePair.Split('=')[0]);
                string value = EncodeDecode.UrlDecode(nameValuePair.Split('=')[1].Trim().Trim('\0')).Trim('\0');

                // skip keys from UploadFileItems.
                if ( files.ContainsKey(name) )
                {
                    UploadFileInfo fileInfo = (UploadFileInfo)files[name];
                    //-----------------------------7d52903b1507ec
                    //\r\n
                    //Content-Disposition: form-data; name=\"oFile1\"; filename=\"C:\\images\\GB Splash Screen.psd\"
                    //\r\n
                    //Content-Type: application/octet-stream
                    //\r\n
                    //\r\n
                    mimeData.Append("--");
                    mimeData.Append(boundary);
                    mimeData.Append("\r\n");
                    mimeData.AppendFormat("Content-Disposition: form-data; name=\"{0}\"",name);
                    mimeData.AppendFormat("; filename=\"{0}\"",fileInfo.FileName);
                    mimeData.Append("\r\n");
                    mimeData.Append("Content-Type: ");
                    mimeData.Append(fileInfo.ContentType);
                    mimeData.Append("\r\n");
                    mimeData.Append("\r\n");
                }
                else
                {
                    // Example
                    //-----------------------------7d52903b1507ec
                    //\r\n
                    //Content-Disposition: form-data;name="Hello23"
                    //\r\n
                    //\r\n
                    //Hello
                    //\r\n

                    mimeData.Append("--");
                    mimeData.Append(boundary);
                    mimeData.Append("\r\n");
                    mimeData.AppendFormat("Content-Disposition: form-data; name=\"{0}\"",name);
                    mimeData.Append("\r\n\r\n");
                    mimeData.Append(value);
                    mimeData.Append("\r\n");
                }
            }

            //mimeData.Append("\0");

            return mimeData.ToString();
        }
 /// <summary>
 /// Closes the command.
 /// </summary>
 public void Close()
 {
     _sessionScripting = null;
     parser = null;
 }