static void Main(string[] args)
        {
            Console.WriteLine("libcurlFtpsExample...");

            const string url = "ftp://*****:*****@ftp.somesite.com/dir/";

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

                Easy easy = new Easy();
                if (easy != null)
                {
                    easy.SetOpt(CURLoption.CURLOPT_URL, url);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false);
                    easy.SetOpt(CURLoption.CURLOPT_FTP_SSL, CURLftpSSL.CURLFTPSSL_TRY);

                    // For debugging will print headers to console.
                    Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);
                    easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);

                    // For debugging will print received data to console.
                    Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
                    easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
                    easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);

                    // List directory only
                    easy.SetOpt(CURLoption.CURLOPT_FTPLISTONLY, true);

                    CURLcode code = easy.Perform();
                    if (code != CURLcode.CURLE_OK)
                    {
                        Console.WriteLine("Request failed!");
                    }

                    easy.Cleanup();
                }
                else
                {
                    Console.WriteLine("Failed to get Easy libcurl handle!");
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
            finally
            {
                Curl.GlobalCleanup();
            }
        }
Пример #2
0
    public static void Main(String[] args)
    {
        try {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);

            easy.SetOpt(CURLoption.CURLOPT_URL, args[0]);
            easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);
            easy.Perform();
            easy.Cleanup();

            Curl.GlobalCleanup();
        }
        catch(Exception ex) {
            Console.WriteLine(ex);
        }
    }
Пример #3
0
    public static void Main(String[] args)
    {
        try {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);

            easy.SetOpt(CURLoption.CURLOPT_URL, args[0]);
            easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);
            easy.Perform();
            easy.Cleanup();

            Curl.GlobalCleanup();
        }
        catch (Exception ex) {
            Console.WriteLine(ex);
        }
    }
Пример #4
0
        /// <summary>
        /// Universal method to load page
        /// </summary>
        /// <param name="url">url to load. It is ignored if form is not null.</param>
        /// <param name="form">form to submit. Should be null if url is not null.</param>
        /// <param name="send_cookies"></param>
        /// <returns></returns>
        CURLcode load_page(string url, bool send_cookies)
        {
            try
            {
                if (Cache.GetCachedFile(ref url, out WR.HtmlResult, out WR.ResponseUrl))
                {
                    WR.web_routine_status = WebRoutineStatus.CACHED;
                    WR.log.Write("From cache: " + url);
                    return(CURLcode.CURLE_OK);
                }

                if (UseCommonCookieFile)
                {
                    copy_common_cookie_file2thread_cookie_file();
                }

                result_ms.Position = 0;
                result_sb.Length   = 0;
                encode_type        = EncodeType.NONE;
                easy            = new Easy();
                content_length  = -1;
                WR.ErrorMessage = null;
                WR.HtmlResult   = null;
                //WR.saved_file = null;
                WR.log.Write("Downloading:" + url);
                if (WR.BTC != null)
                {
                    WR.BTC.BTS.ProgressMax   = 0;
                    WR.BTC.BTS.ProgressValue = 100;
                    WR.BTC.BTS.Status        = "Sleeping Crawl Interval: " + Config.Web.CrawlTimeIntervalInMss.ToString();
                    WR.BTC.DisplayStatus();
                    Thread.Sleep(Config.Web.CrawlTimeIntervalInMss);
                    WR.BTC.BTS.Status = "URL:" + url;
                    WR.BTC.DisplayStatus();
                }
                WR.web_routine_status = WebRoutineStatus.UNDEFINED;

                //if (flagReceiveDebugMessages)
                //{
                //    Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
                //    easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
                //    easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
                //}

                Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteStringBuilder);
                easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);

                easy.SetOpt(CURLoption.CURLOPT_URL, url);

                if (url.StartsWith("https"))
                {
                    easy.SetOpt(CURLoption.CURLOPT_CAINFO, "ca-bundle.crt");
                }

                Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData);
                easy.SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf);

                Slist sl = new Slist();
                sl.Append("Accept: " + WR.HttpRequestAcceptHeader);
                sl.Append("Referer: " + WR.HttpRequestRefererHeader);
                sl.Append("Accept-Encoding: gzip, deflate");
                easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, sl);

                easy.SetOpt(CURLoption.CURLOPT_USERAGENT, Config.Web.HttpUserAgent);

                easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile);

                if (send_cookies)
                {
                    easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile);
                }

                if (WR.MaxAutoRedirectionCount >= 0)
                {
                    easy.SetOpt(CURLoption.CURLOPT_MAXREDIRS, WR.MaxAutoRedirectionCount);
                }

                //Easy.ProgressFunction pf = new Easy.ProgressFunction(OnProgress);
                //easy.SetOpt(CURLoption.CURLOPT_PROGRESSDATA, );

                easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);

                if (proxy != null)
                {
                    easy.SetOpt(CURLoption.CURLOPT_PROXY, proxy.Ip);
                    easy.SetOpt(CURLoption.CURLOPT_PROXYPORT, proxy.Port);

                    easy.SetOpt(CURLoption.CURLOPT_PROXYAUTH, CURLhttpAuth.CURLAUTH_ANY);
                    if (proxy.Login != "" || proxy.Password != "")
                    {
                        easy.SetOpt(CURLoption.CURLOPT_PROXYUSERPWD, proxy.Login + ":" + proxy.Password);
                    }
                    if (proxy.Type == ProxyType.SOCKS5)
                    {
                        easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_SOCKS5);
                    }
                    else if (proxy.Type == ProxyType.SOCKS4)
                    {
                        easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_SOCKS4);
                    }
                    else
                    {
                        easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_HTTP);
                    }
                    easy.SetOpt(CURLoption.CURLOPT_PROXYAUTH, CURLhttpAuth.CURLAUTH_ANY);
                }
                if (WR.BTC != null)
                {
                    if (proxy != null)
                    {
                        WR.BTC.BTS.Proxy = proxy.Ip + ":" + proxy.Port;
                    }
                    else
                    {
                        WR.BTC.BTS.Proxy = "NOT USED";
                    }
                    WR.BTC.DisplayStatus();
                }

                easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, Config.Web.HttpRequestTimeoutInSeconds);

                CURLcode rc = easy.Perform();

                easy.GetInfo(CURLINFO.CURLINFO_EFFECTIVE_URL, ref WR.ResponseUrl);

                easy.Cleanup();

                WR.HtmlResult = result_sb.ToString();

                if (rc != CURLcode.CURLE_OK)
                {
                    WR.ErrorMessage       = easy.StrError(rc);
                    WR.web_routine_status = WebRoutineStatus.DOWNLOAD_ERROR;
                    WR.log.Error("Download error: " + WR.ErrorMessage + "\nURL:" + url + "\nPROXY: " + proxy.Ip + ":" + proxy.Port);
                    return(rc);
                }

                if (UseCommonCookieFile)
                {
                    copy_thread_cookie_file2common_cookie_file();
                }

                if (WR.web_routine_status == WebRoutineStatus.UNDEFINED)
                {
                    WR.web_routine_status = WebRoutineStatus.OK;
                }

                WR.log.CacheDownloadedString(ref url, ref WR.ResponseUrl, ref WR.HtmlResult, WR.page_number++, WR.CycleIdentifier);

                return(rc);
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception error)
            {
                WR.web_routine_status = WebRoutineStatus.EXCEPTION;
                WR.log.Error(error.Message + "\nURL: " + url + "\nPROXY: " + proxy.Ip + ":" + proxy.Port);

                if (easy != null)
                {
                    easy.Cleanup();
                }
            }

            return(CURLcode.CURLE_FAILED_INIT);
        }