public static void Main(String[] args) { try { Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); Easy easy = new Easy(); Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); 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, args[0]); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); easy.Perform(); easy.Cleanup(); Curl.GlobalCleanup(); } catch(Exception ex) { Console.WriteLine(ex); } }
public static void Main(String[] args) { try { Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); Easy easy = new Easy(); Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); 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, "https://dictionary.cambridge.org/grammar/british-grammar/above-or-over"); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); easy.Perform(); //easy.Cleanup(); //easy.Dispose(); Curl.GlobalCleanup(); Console.WriteLine("Enter to exit ..."); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex); } }
public static void Main(String[] args) { try { Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); Easy easy = new Easy(); Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); 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, args[0]); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); easy.Perform(); easy.Cleanup(); Curl.GlobalCleanup(); } catch (Exception ex) { Console.WriteLine(ex); } }
public static string f_https_getTextByUrl(string url) { var dataRecorder = new EasyDataRecorder(); Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_DEFAULT); try { using (Easy easy = new Easy()) { easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, (Easy.WriteFunction)dataRecorder.HandleWrite); 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.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); easy.Perform(); } } finally { Curl.GlobalCleanup(); } string s = Encoding.UTF8.GetString(dataRecorder.Written.ToArray()); return(s); }
static void post1() { URL = "https://azure.microsoft.com/en-us/cognitive-services/demo/websearchapi/"; Console.WriteLine("POST: " + URL); File.WriteAllText("libcurl-post.txt", string.Empty); var dataRecorder = new EasyDataRecorder(); Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_DEFAULT); try { using (Easy easy = new Easy()) { easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, (Easy.WriteFunction)dataRecorder.HandleWrite); Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext); easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf); //POST easy.SetOpt(CURLoption.CURLOPT_URL, URL); /* use a POST to fetch this */ //easy.SetOpt(CURLoption.CURLOPT_HTTPPOST, 1L); string coo = File.ReadAllText("cookies.txt").Trim(); string __RequestVerificationToken = string.Empty; if (coo.Contains("__RequestVerificationToken")) { __RequestVerificationToken = coo.Split(new string[] { "__RequestVerificationToken" }, StringSplitOptions.None)[1].Trim(); } /* Now specify the POST data */ easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, "__RequestVerificationToken=" + __RequestVerificationToken + "&Query=english+due+to+tienganh123&Market=en-us&Safesearch=Strict&freshness="); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); //read cookie easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, "cookies.txt"); easy.Perform(); } } finally { Curl.GlobalCleanup(); } string s = Encoding.UTF8.GetString(dataRecorder.Written.ToArray()); //Console.WriteLine(s); File.WriteAllText("libcurl-post.txt", s); Console.WriteLine("\r\n[END-POST]"); }
static void get1() { //URL = "http://httpbin.org/"; //URL = "https://vnexpress.net/"; URL = "https://azure.microsoft.com/en-us/services/cognitive-services/bing-web-search-api/"; Console.WriteLine("GET: " + URL); File.WriteAllText("libcurl-get.txt", string.Empty); var dataRecorder = new EasyDataRecorder(); Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_DEFAULT); try { using (Easy easy = new Easy()) { easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, (Easy.WriteFunction)dataRecorder.HandleWrite); /* example.com is redirected, so we tell libcurl to follow redirection */ easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, 1L); Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext); easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf); easy.SetOpt(CURLoption.CURLOPT_URL, URL); /* use a GET to fetch this */ //easy.SetOpt(CURLoption.CURLOPT_HTTPGET, 1L); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); //write cookie easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, "cookies.txt"); easy.Perform(); } } finally { Curl.GlobalCleanup(); } string s = Encoding.UTF8.GetString(dataRecorder.Written.ToArray()); //Console.WriteLine(s); File.WriteAllText("libcurl-get.txt", s); Console.WriteLine("\r\n[END-GET]"); }
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()); }
public static void Main(string[] args) { TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8888); server.Start(); Console.WriteLine("Server has started on 127.0.0.1:80.{0}Waiting for a connection...", Environment.NewLine); while (true) { TcpClient client = server.AcceptTcpClient(); Console.WriteLine("A client connected."); NetworkStream stream = client.GetStream(); while (client.Available < 3) { Console.WriteLine("::> wait for enough bytes to be available"); } Byte[] bytes = new Byte[client.Available]; stream.Read(bytes, 0, bytes.Length); //translate bytes of request to string string _request = Encoding.UTF8.GetString(bytes), line_first = _request.Split(new string[] { "\r\n" }, StringSplitOptions.None)[0].Trim(), url = line_first.Replace("GET ", string.Empty).Replace("POST ", string.Empty).Trim().Split(' ')[0]; Console.WriteLine("-> " + url); //string res = addHTTPHeader("12345"); //Byte[] bSendData = Encoding.ASCII.GetBytes(res); //stream.Write(bSendData, 0, bSendData.Length); if (_request.Contains("text/html") == false) { string res = addHTTPHeader(0, ""); Byte[] bSendData = Encoding.ASCII.GetBytes(res); stream.Write(bSendData, 0, bSendData.Length); stream.Flush(); } else { string URL = "https://dictionary.cambridge.org/grammar/british-grammar/above-or-over"; URL = "https://azure.microsoft.com/en-us/services/cognitive-services/bing-web-search-api/"; var dataRecorder = new EasyDataRecorder(); Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_DEFAULT); try { using (Easy easy = new Easy()) { //easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, (Easy.HeaderFunction)dataRecorder.HandleHeader); //easy.SetOpt(CURLoption.CURLOPT_HEADER, false); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, (Easy.WriteFunction)dataRecorder.HandleWrite); Easy.SSLContextFunction sf = new Easy.SSLContextFunction(OnSSLContext); easy.SetOpt(CURLoption.CURLOPT_SSL_CTX_FUNCTION, sf); easy.SetOpt(CURLoption.CURLOPT_URL, URL); /* example.com is redirected, so we tell libcurl to follow redirection */ //easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, 1L); easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt"); easy.Perform(); } } finally { Curl.GlobalCleanup(); } byte[] bufHeader = dataRecorder.Header.ToArray(); byte[] bufBody = dataRecorder.Written.ToArray(); string header = dataRecorder.HeaderAsString, body = Encoding.UTF8.GetString(bufBody); //state.SourceSocket.Send(bufHeader, 0, bufHeader.Length, SocketFlags.None); Console.WriteLine("OK> " + URL); File.WriteAllText("text.txt", body); Byte[] bSendData = Encoding.UTF8.GetBytes(addHTTPHeader(bufBody.Length, body)); stream.Write(bSendData, 0, bSendData.Length); stream.Flush(); } } Console.ReadLine(); }
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); }
//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); } }
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); } }
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); } }