Exemplo n.º 1
0
        public static void TestPOSTRequest()
        {
            WebRequest   request;
            WebResponse  response;
            StreamReader reader;
            GZipStream   gzipStream;
            string       responseText;

            MSHTML.HTMLDocument   htmlresponse = new MSHTML.HTMLDocument();
            MSHTML.IHTMLDocument2 webresponse  = (MSHTML.IHTMLDocument2)htmlresponse;
            string body = "curr_id=24442&smlID=1169009&st_date=11%2F18%2F2016&end_date=11%2F18%2F2017&interval_sec=Daily&sort_col=date&sort_ord=DESC&action=historical_data";

            try
            {
                request             = WebRequest.Create("https://ca.investing.com/instruments/HistoricalDataAjax");
                request.Credentials = CredentialCache.DefaultCredentials;
                ((HttpWebRequest)request).Accept    = "*/*";
                ((HttpWebRequest)request).Method    = "POST";
                ((HttpWebRequest)request).UserAgent = DEFAULT_USER_AGENT;
                ((HttpWebRequest)request).Referer   = "http://ca.investing.com/equities/canada";
                ((HttpWebRequest)request).Headers.Add("Accept-Encoding", "gzip,deflate");
                ((HttpWebRequest)request).Headers.Add("X-Requested-With", "XMLHttpRequest");
                ((HttpWebRequest)request).Timeout = 20000;

                request.ContentType   = "application/x-www-form-urlencoded; charset=UTF-8";
                request.ContentLength = body.Length;

                Stream rStream = request.GetRequestStream();
                rStream.Write(body.Select(c => (byte)c).ToArray(), 0, body.Length);

                response = request.GetResponse();

                if (response.Headers.Get("Content-Encoding").ToLower().Contains("gzip"))
                {
                    gzipStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                    reader     = new StreamReader(gzipStream);
                }
                else
                {
                    reader = new StreamReader(response.GetResponseStream());
                }
                responseText = reader.ReadToEnd();
                webresponse.write(responseText);

                while (webresponse.body == null)
                {
                    System.Windows.Forms.Application.DoEvents();
                }

                webresponse.close();

                reader.Close();
                response.Close();
            }
            catch (Exception)
            {
                /* Do nothing */
                //int stophere = 1;
            }
        }
Exemplo n.º 2
0
        public static MSHTML.IHTMLDocument2 HTMLRequestResponse(String pUrl)
        {
            WebRequest   request;
            WebResponse  response;
            StreamReader reader;
            GZipStream   gzipStream;
            string       responseText;

            MSHTML.HTMLDocument   htmlresponse = new MSHTML.HTMLDocument();
            MSHTML.IHTMLDocument2 webresponse  = (MSHTML.IHTMLDocument2)htmlresponse;

            try
            {
                if (pUrl != "")
                {
                    request             = WebRequest.Create(pUrl);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    ((HttpWebRequest)request).Accept    = "*/*";
                    ((HttpWebRequest)request).UserAgent = DEFAULT_USER_AGENT;
                    ((HttpWebRequest)request).Referer   = "http://ca.investing.com/equities/canada";
                    ((HttpWebRequest)request).Headers.Add("Accept-Encoding", "gzip,deflate,br");
                    ((HttpWebRequest)request).Headers.Add("X-Requested-With", "XMLHttpRequest");
                    ((HttpWebRequest)request).KeepAlive = true;
                    ((HttpWebRequest)request).Timeout   = 20000;

                    response = request.GetResponse();

                    if (response.Headers.Get("Content-Encoding").ToLower().Contains("gzip"))
                    {
                        gzipStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                        reader     = new StreamReader(gzipStream);
                    }
                    else
                    {
                        reader = new StreamReader(response.GetResponseStream());
                    }
                    responseText = reader.ReadToEnd();
                    webresponse.write(responseText);

                    while (webresponse.body == null)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }

                    webresponse.close();

                    reader.Close();
                    response.Close();
                }
            }
            catch (Exception)
            {
                /* Do nothing */
                //int stophere = 1;
            }

            return(webresponse);
        }
Exemplo n.º 3
0
        /*****************************************************************************
        *  FUNCTION:       updateDataManagerConfig
        *  Description:
        *  Parameters:     None
        *****************************************************************************/
        private bool updateDataManagerConfig()
        {
            bool success = true;
            int  index1, index2;

            //Web Request / Response locals
            String      baseUrl, subUrl, responseText, parseText;
            WebRequest  request;
            WebResponse response;

            MSHTML.HTMLDocument   htmlresponse = new MSHTML.HTMLDocument();
            MSHTML.IHTMLDocument2 webresponse = (MSHTML.IHTMLDocument2)htmlresponse;

            //Xml objects
            XmlDocument  configFile = new XmlDocument();
            XmlNodeList  nodeList, keyNodeList, subNodeList, configMarketsNodeList, configOptionNodeList;
            XmlDocument  KeyElement = new XmlDocument();
            XmlNode      marketNode;
            StreamReader reader;

            List <String> listOfMarkets;
            List <String> listOfOptions;

            string application_path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string config_path      = Path.Combine(application_path, MyMarketAnalyzer.Properties.Resources.DataManagerConfigPath);

            config_path = new Uri(config_path).LocalPath;

            configFile.Load(config_path);

            nodeList = configFile.SelectNodes("Datamanager/Live/Markets/Schema");

            index1    = -1;
            index2    = -1;
            parseText = "";
            try
            {
                baseUrl             = nodeList[0].Attributes.GetNamedItem("url").InnerText;
                request             = WebRequest.Create(baseUrl);
                request.Credentials = CredentialCache.DefaultCredentials;
                ((HttpWebRequest)request).Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                ((HttpWebRequest)request).UserAgent = DEFAULT_USER_AGENT;
                ((HttpWebRequest)request).Timeout   = 20000;

                response = request.GetResponse();

                reader       = new StreamReader(response.GetResponseStream());
                responseText = reader.ReadToEnd();
                reader.Close();
                response.Close();

                //write response to string
                webresponse.write(responseText);

                //begin search for the menu items we want
                keyNodeList = GetConfiguredItems(responseText, configFile, "Datamanager/Live/Markets/Schema/");

                configMarketsNodeList = configFile.SelectNodes("Datamanager/Live/Markets/Data/Market");
                listOfMarkets         = new List <string>();

                for (int i = 0; i < configMarketsNodeList.Count; i++)
                {
                    listOfMarkets.Add(configMarketsNodeList[i].Attributes.GetNamedItem("name").InnerText);
                }

                //For each geographic location, get the list of available markets / indexes
                foreach (XmlNode subNode in keyNodeList)
                {
                    if (subNode.InnerXml != "")
                    {
                        index1    = subNode.OuterXml.IndexOf("href=\"", 0) + 6;
                        index2    = subNode.OuterXml.IndexOf("\"", index1);
                        parseText = subNode.OuterXml.Substring(index1, index2 - index1);
                        subUrl    = (new Uri(new Uri(baseUrl), parseText)).ToString();

                        if (parseText.Substring(0, 1) != "/")
                        {
                            parseText = "/" + parseText;
                        }

                        if (listOfMarkets.Contains(subNode.InnerXml))
                        {
                            marketNode = configMarketsNodeList[listOfMarkets.IndexOf(subNode.InnerXml)];
                        }
                        else
                        {
                            marketNode = configFile.CreateNode("element", "Market", "");

                            XmlAttribute atHref = configFile.CreateAttribute("href");
                            atHref.Value = parseText;

                            XmlAttribute atName = configFile.CreateAttribute("name");
                            atName.Value = subNode.InnerText;

                            marketNode.Attributes.Append(atHref);
                            marketNode.Attributes.Append(atName);

                            configFile.SelectNodes("Datamanager/Live/Markets/Data")[0].InsertAfter(marketNode, configMarketsNodeList[configMarketsNodeList.Count - 1]);
                        }

                        responseText        = "";
                        request             = WebRequest.Create(subUrl);
                        request.Credentials = CredentialCache.DefaultCredentials;
                        ((HttpWebRequest)request).Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                        ((HttpWebRequest)request).UserAgent = DEFAULT_USER_AGENT;
                        ((HttpWebRequest)request).Timeout   = 20000;

                        response = request.GetResponse();

                        reader       = new StreamReader(response.GetResponseStream());
                        responseText = reader.ReadToEnd();
                        reader.Close();
                        response.Close();

                        //write response to string
                        webresponse.write(responseText);

                        subNodeList          = GetConfiguredItems(responseText, configFile, "Datamanager/Live/Markets/Data/Schema/");
                        listOfOptions        = new List <string>();
                        configOptionNodeList = null;

                        if (marketNode.HasChildNodes)
                        {
                            configOptionNodeList = marketNode.SelectNodes("option");

                            for (int i = 0; i < configOptionNodeList.Count; i++)
                            {
                                listOfOptions.Add(configOptionNodeList[i].Attributes.GetNamedItem("id").InnerText);
                            }
                        }

                        foreach (XmlNode optionNode in subNodeList)
                        {
                            //Only add options that aren't already in the list (based on id)
                            if (!listOfOptions.Contains(optionNode.Attributes.GetNamedItem("id").InnerText))
                            {
                                XmlNode newOption = configFile.ImportNode(optionNode, true);
                                if (configOptionNodeList == null)
                                {
                                    marketNode.AppendChild(newOption);
                                }
                                else
                                {
                                    marketNode.InsertAfter(newOption, configOptionNodeList[configOptionNodeList.Count - 1]);
                                }
                            }
                        }
                    }
                }

                configFile.Save(config_path);
            }
            catch (Exception e)
            {
                success = false;
            }

            return(success);
        }