Example #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;
            }
        }
Example #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);
        }