Exemplo n.º 1
0
        public string Perform()
        {
            CURLcode data = easy.Perform();

            easy.Dispose();
            return(buffer);
        }
Exemplo n.º 2
0
        public static oResult test_http(Dictionary <string, object> request = null)
        {
            oResult rs = new oResult()
            {
                ok = false, request = request
            };

            string url = request.getValue <string>("url");

            if (string.IsNullOrWhiteSpace(url))
            {
                url = "http://localhost/";
            }

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy               easy = new Easy();
                StringBuilder      bi   = new StringBuilder();
                Easy.WriteFunction wf   = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                easy.Perform();
                //easy.Cleanup();
                easy.Dispose();

                Curl.GlobalCleanup();

                rs.data = bi.ToString();
                rs.ok   = true;
                rs.type = DATA_TYPE.HTML_TEXT;
            }
            catch (Exception ex)
            {
                rs.error = ex.Message;
            }

            return(rs);
        }
Exemplo n.º 3
0
        public static string getString(string url)
        {
            StringBuilder bi = new StringBuilder();

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy = new Easy();

                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    bi.Append(System.Text.Encoding.UTF8.GetString(buf));
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");

                easy.Perform();
                //easy.Cleanup();
                easy.Dispose();

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
                bi.Append(url);
                bi.Append(Environment.NewLine);
                bi.Append(ex.Message);
                bi.Append(Environment.NewLine);
                bi.Append(ex.StackTrace);
            }
            return(bi.ToString());
        }
Exemplo n.º 4
0
        public static string get_raw_http(object p)
        {
            if (p == null)
            {
                p = "http://localhost/";
            }

            string        url = p.ToString();
            StringBuilder bi  = new StringBuilder();

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy             = new Easy();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                easy.Perform();
                //easy.Cleanup();
                easy.Dispose();

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(bi.ToString());
        }
Exemplo n.º 5
0
        public static string Send(string ip, string type, string cmd)
        {
            IPAddress ipaddress;
            //Console.WriteLine("Enter IP Address");
            //string ipaddress = Console.ReadLine();
            bool ValidateIP = IPAddress.TryParse(ip, out ipaddress);

            if (ValidateIP)
            {
                string Isps4 = "";

                Ping      myPing = new Ping();
                PingReply reply  = myPing.Send(ip, 1000);
                if (reply != null)
                {
                    Isps4 = reply.Status.ToString();
                }

                if (Isps4 == "TimedOut")
                {
                    out1 = "TimedOut";
                }

                if (Isps4 != "TimedOut")
                {
                    try
                    {
                        Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                        Easy easy = new Easy();
                        out1 = "";
                        Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData);
                        easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                        // simple post - with a string
                        easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, cmd);//"{\"title_id\":\"CUSA09311\"}";

                        easy.SetOpt(CURLoption.CURLOPT_USERAGENT,
                                    "Mozilla 4.0 (compatible; MSIE 6.0; Win32");
                        easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);

                        easy.SetOpt(CURLoption.CURLOPT_URL, "http://" + ip + ":12800" + "/api/" + type); //"http://192.168.1.15:12800/api/is_exists");

                        easy.SetOpt(CURLoption.CURLOPT_POST, true);

                        easy.Perform();
                        easy.Dispose();

                        Curl.GlobalCleanup();
                        return(out1);
                    }
                    catch (Exception ex)
                    {
                        return(out1);
                        //Console.WriteLine(ex);
                    }
                }
            }
            else
            {
                out1 = "BadIP";
            }
            return(out1);
        }
Exemplo n.º 6
0
        public static oResult test_https(Dictionary <string, object> request = null)
        {
            oResult rs = new oResult()
            {
                ok = false, request = request
            };

            string url = request.getValue <string>("url");

            if (string.IsNullOrWhiteSpace(url))
            {
                url = "https://vnexpress.net/ha-noi-de-xuat-keo-dai-cach-ly-xa-hoi-den-30-4-4084947.html";
            }

            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy = new Easy();

                StringBuilder      bi = new StringBuilder();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction((ctx, extraData) => CURLcode.CURLE_OK);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                //easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
                string file_crt = Path.Combine(_CONFIG.PATH_ROOT, @"bin\ca-bundle.crt");
                if (File.Exists(file_crt) == false)
                {
                    rs.error = "Cannot found file: " + file_crt;
                    return(rs);
                }
                easy.SetOpt(CURLoption.CURLOPT_CAINFO, file_crt);

                easy.Perform();
                easy.Dispose();

                Curl.GlobalCleanup();

                string s = bi.ToString();

                //string title = Regex.Match(s, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

                s = new Regex(@"<script[^>]*>[\s\S]*?</script>").Replace(s, string.Empty);
                s = new Regex(@"<style[^>]*>[\s\S]*?</style>").Replace(s, string.Empty);
                s = new Regex(@"<noscript[^>]*>[\s\S]*?</noscript>").Replace(s, string.Empty);
                s = Regex.Replace(s, @"<meta(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<link(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<use(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<figure(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!DOCTYPE(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!--(.|\n)*?-->", string.Empty, RegexOptions.Singleline);

                s = Regex.Replace(s, @"(?:\r\n|\r(?!\n)|(?<!\r)\n){2,}", "\r\n");

                rs.data = s;
                rs.ok   = true;
                rs.type = DATA_TYPE.HTML_TEXT;
            }
            catch (Exception ex)
            {
                rs.error = "ERR: " + ex.Message;
            }

            return(rs);
        }
Exemplo n.º 7
0
        //static bool curl_inited = false;
        public static string get_raw_https(object p)
        {
            //if (p == null || string.IsNullOrWhiteSpace(p.ToString())) return string.Empty;

            if (p == null)
            {
                p = "https://vnexpress.net/ha-noi-de-xuat-keo-dai-cach-ly-xa-hoi-den-30-4-4084947.html";
            }

            string url = p.ToString();

            try
            {
                //if (curl_inited == false)
                //{
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                //    curl_inited = true;
                //}

                Easy easy = new Easy();

                StringBuilder      bi = new StringBuilder();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction((ctx, extraData) => CURLcode.CURLE_OK);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                //easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
                easy.SetOpt(CURLoption.CURLOPT_CAINFO, @"D:\cache_redis\ckv_lib\bin\ca-bundle.crt");

                easy.Perform();
                easy.Dispose();

                //Curl.GlobalCleanup();

                string s = bi.ToString();

                //string title = Regex.Match(s, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

                s = new Regex(@"<script[^>]*>[\s\S]*?</script>").Replace(s, string.Empty);
                s = new Regex(@"<style[^>]*>[\s\S]*?</style>").Replace(s, string.Empty);
                s = new Regex(@"<noscript[^>]*>[\s\S]*?</noscript>").Replace(s, string.Empty);
                s = Regex.Replace(s, @"<meta(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<link(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<use(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<figure(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!DOCTYPE(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!--(.|\n)*?-->", string.Empty, RegexOptions.Singleline);

                s = Regex.Replace(s, @"(?:\r\n|\r(?!\n)|(?<!\r)\n){2,}", "\r\n");

                return(s);
            }
            catch (Exception ex)
            {
                return("ERR: " + ex.Message);
            }
        }
Exemplo n.º 8
0
        public static string ___https(string url)
        {
            try
            {
                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

                Easy easy = new Easy();

                StringBuilder      bi = new StringBuilder();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction((ctx, extraData) => CURLcode.CURLE_OK);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                //easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
                string file_crt = Path.Combine(_CONFIG.PATH_ROOT, @"bin\ca-bundle.crt");
                if (File.Exists(file_crt) == false)
                {
                    return("ERR: Cannot found file: " + file_crt);
                }

                easy.SetOpt(CURLoption.CURLOPT_CAINFO, file_crt);

                easy.Perform();

                Thread.Sleep(3000);

                easy.Dispose();

                Curl.GlobalCleanup();

                string s = bi.ToString();

                //string title = Regex.Match(s, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

                //s = new Regex(@"<script[^>]*>[\s\S]*?</script>").Replace(s, string.Empty);
                //s = new Regex(@"<style[^>]*>[\s\S]*?</style>").Replace(s, string.Empty);
                //s = new Regex(@"<noscript[^>]*>[\s\S]*?</noscript>").Replace(s, string.Empty);
                //s = Regex.Replace(s, @"<meta(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                //s = Regex.Replace(s, @"<link(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                //s = Regex.Replace(s, @"<use(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                //s = Regex.Replace(s, @"<figure(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                //s = Regex.Replace(s, @"<!DOCTYPE(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                //s = Regex.Replace(s, @"<!--(.|\n)*?-->", string.Empty, RegexOptions.Singleline);

                //s = Regex.Replace(s, @"(?:\r\n|\r(?!\n)|(?<!\r)\n){2,}", "\r\n");

                return(s);
            }
            catch (Exception ex)
            {
                return("ERR: " + ex.Message);
            }
        }
Exemplo n.º 9
0
        static string curl_get_raw(object p)
        {
            if (p == null)
            {
                return(string.Empty);
            }

            string url = p.ToString();

            try
            {
                if (curl_inited == false)
                {
                    Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                    curl_inited = true;
                }

                Easy easy = new Easy();

                StringBuilder      bi = new StringBuilder();
                Easy.WriteFunction wf = new Easy.WriteFunction((buf, size, nmemb, extraData) =>
                {
                    string si = Encoding.UTF8.GetString(buf);
                    bi.Append(si);
                    return(size * nmemb);
                });
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                Easy.SSLContextFunction sf = new Easy.SSLContextFunction((ctx, extraData) => CURLcode.CURLE_OK);
                easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);
                easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");

                easy.Perform();
                easy.Dispose();

                //Curl.GlobalCleanup();

                string s = bi.ToString();

                //string title = Regex.Match(s, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

                s = new Regex(@"<script[^>]*>[\s\S]*?</script>").Replace(s, string.Empty);
                s = new Regex(@"<style[^>]*>[\s\S]*?</style>").Replace(s, string.Empty);
                s = new Regex(@"<noscript[^>]*>[\s\S]*?</noscript>").Replace(s, string.Empty);
                s = Regex.Replace(s, @"<meta(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<link(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<use(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<figure(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!DOCTYPE(.|\n)*?>", string.Empty, RegexOptions.Singleline);
                s = Regex.Replace(s, @"<!--(.|\n)*?-->", string.Empty, RegexOptions.Singleline);

                s = Regex.Replace(s, @"(?:\r\n|\r(?!\n)|(?<!\r)\n){2,}", "\r\n");

                return(s);
            }
            catch (Exception ex)
            {
                return("ERR: " + ex.Message);
            }
        }