Exemple #1
0
        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]");
        }
Exemple #2
0
        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]");
        }
Exemple #3
0
        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();
        }