Implements the curl_easy_xxx API.
This is the most important class in libcurl.NET. It wraps a CURL* handle and provides delegates through which callbacks (such as CURLOPT_WRITEFUNCTION and CURLOPT_READFUNCTION) are implemented.
Exemplo n.º 1
1
        public static void PerformPostMultipartRequest(string query, string postData)
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();

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

            // simple post - with a string
            easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS,
                postData);

            easy.SetOpt(CURLoption.CURLOPT_USERAGENT,
                "C# EchoNest Lib");
            easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
            easy.SetOpt(CURLoption.CURLOPT_URL,
                query);
            easy.SetOpt(CURLoption.CURLOPT_POST, true);

            CURLcode res = easy.Perform();

            if (res != CURLcode.CURLE_OK)
                throw new WebException("Post operation failed");

            easy.Cleanup();

            Curl.GlobalCleanup();
        }
Exemplo n.º 2
1
        /// <summary>
        /// Constructor, init curl service.
        /// </summary>
        public Curl(string serverURL)
        {
            _serverURL = serverURL;
            LibCurl.Curl.GlobalInit((int)LibCurl.CURLinitFlag.CURL_GLOBAL_DEFAULT);
            easy = new LibCurl.Easy();
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_TIMEOUT, 300);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_COOKIEFILE,Enums.COOKIESFILE);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_COOKIEJAR, Enums.COOKIESFILE);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_FOLLOWLOCATION, true);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_POST, true);

            LibCurl.Easy.DebugFunction df = new LibCurl.Easy.DebugFunction(OnDebug);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_DEBUGFUNCTION, df);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_VERBOSE, true);

            LibCurl.Easy.ProgressFunction pf = new LibCurl.Easy.ProgressFunction(OnProgress);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_PROGRESSFUNCTION, pf);

            wf = new LibCurl.Easy.WriteFunction(OnWriteData);
        }
Exemplo n.º 3
1
        public static bool gopost(string url, string user, string password,  string data, TradeLink.API.DebugDelegate deb, out string result)
        {
            debs = deb;
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            rresult = new StringBuilder();
            hasresult = false;

            Easy.WriteFunction wf = new Easy.WriteFunction(OnWritePostData);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
            easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);

            // simple post - with a string
            easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS,
                data);
            Slist sl = new Slist();
            sl.Append("Content-Type:application/xml");
            sl.Append("Accept: application/xml");
            easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, sl);
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
            easy.SetOpt(CURLoption.CURLOPT_USERAGENT,
                "Mozilla 4.0 (compatible; MSIE 6.0; Win32");
            easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
            easy.SetOpt(CURLoption.CURLOPT_USERPWD, user + ":" + password);
            CURLhttpAuth authflag = CURLhttpAuth.CURLAUTH_BASIC;
            easy.SetOpt(CURLoption.CURLOPT_HTTPAUTH, authflag);
            easy.SetOpt(CURLoption.CURLOPT_URL,url);
            
            easy.SetOpt(CURLoption.CURLOPT_POST, true);
            if (debs!=null)
                easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
            


            CURLcode err = easy.Perform();
            int waits = 0;
            int maxwaits = 200;
            while (!hasresult && (waits++<maxwaits))
                System.Threading.Thread.Sleep(10);

            int rcodei = 0;
            CURLcode rcode = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref rcodei);


            if (!hasresult && (deb != null))
                deb(easy.StrError(err));

            easy.Cleanup();



            Curl.GlobalCleanup();
            result = rresult.ToString();



            return hasresult;
        }
Exemplo n.º 4
0
        // called by libcurlshim
        private static int ReadDelegate(IntPtr buf, int sz, int nmemb,
                                        IntPtr parm)
        {
            int bytes = sz * nmemb;

            byte[]   b    = new byte[bytes];
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            if (easy == null)
            {
                return(0);
            }
            if (easy.m_pfRead == null)
            {
                return(0);
            }
            int nRead = easy.m_pfRead(b, sz, nmemb, easy.m_readData);

            if (nRead > 0)
            {
                for (int i = 0; i < nRead; i++)
                {
                    Marshal.WriteByte(buf, i, b[i]);
                }
            }
            return(nRead);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Remove an Easy object.
        /// </summary>
        /// <param name="easy">
        /// <see cref="Easy"/> object to remove.
        /// </param>
        /// <returns>
        /// A <see cref="CURLMcode"/>, hopefully <c>CURLMcode.CURLM_OK</c>
        /// </returns>
        /// <exception cref="System.NullReferenceException">
        /// This is thrown if the native <c>Multi</c> handle wasn't
        /// created successfully.
        /// </exception>
        public CURLMcode RemoveHandle(Easy easy)
        {
            EnsureHandle();
            IntPtr p = easy.GetHandle();

            m_htEasy.Remove(p);
            return(External.curl_multi_remove_handle(m_pMulti,
                                                     easy.GetHandle()));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add an Easy object.
        /// </summary>
        /// <param name="easy">
        /// <see cref="Easy"/> object to add.
        /// </param>
        /// <returns>
        /// A <see cref="CURLMcode"/>, hopefully <c>CURLMcode.CURLM_OK</c>
        /// </returns>
        /// <exception cref="System.NullReferenceException">
        /// This is thrown if the native <c>Multi</c> handle wasn't
        /// created successfully.
        /// </exception>
        public CURLMcode AddHandle(Easy easy)
        {
            EnsureHandle();
            IntPtr p = easy.GetHandle();

            m_htEasy.Add(p, easy);
            return(External.curl_multi_add_handle(m_pMulti,
                                                  easy.GetHandle()));
        }
Exemplo n.º 7
0
        // called by libcurlshim
        private static CURLIOERR IoctlDelegate(CURLIOCMD cmd,
                                               IntPtr parm)
        {
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            // let's require all of these to be non-null
            if (easy == null || easy.m_pfIoctl == null ||
                easy.m_ioctlData == null)
            {
                return(CURLIOERR.CURLIOE_UNKNOWNCMD);
            }
            return(easy.m_pfIoctl(cmd, easy.m_ioctlData));
        }
Exemplo n.º 8
0
        // called by libcurlshim
        private static int SSLCtxDelegate(IntPtr ctx, IntPtr parm)
        {
            int      ok   = (int)CURLcode.CURLE_OK;
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            if (easy == null)
            {
                return(ok);
            }
            if (easy.m_pfSSLContext == null)
            {
                return(ok);  // keep going
            }
            SSLContext context = new SSLContext(ctx);

            return((int)easy.m_pfSSLContext(context, easy.m_sslContextData));
        }
Exemplo n.º 9
0
        // called by libcurlshim
        private static int ProgressDelegate(IntPtr parm, double dlTotal,
                                            double dlNow, double ulTotal, double ulNow)
        {
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            if (easy == null)
            {
                return(0);
            }
            if (easy.m_pfProgress == null)
            {
                return(0);
            }
            int nprog = easy.m_pfProgress(easy.m_progressData, dlTotal,
                                          dlNow, ulTotal, ulNow);

            return(nprog);
        }
Exemplo n.º 10
0
        // called by libcurlshim
        private static int DebugDelegate(CURLINFOTYPE infoType,
                                         IntPtr msgBuf, int msgBufSize, IntPtr parm)
        {
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            if (easy == null)
            {
                return(0);
            }
            if (easy.m_pfDebug == null)
            {
                return(0);
            }
            String message = Marshal.PtrToStringAnsi(msgBuf, msgBufSize);

            easy.m_pfDebug(infoType, message, easy.m_debugData);
            return(0);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Constructor, init curl service.
        /// </summary>
        public Curl(string serverURL)
        {
            _serverURL = serverURL;
            LibCurl.Curl.GlobalInit((int)LibCurl.CURLinitFlag.CURL_GLOBAL_DEFAULT);
            easy = new LibCurl.Easy();
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_TIMEOUT, 300);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_COOKIEFILE, Enums.COOKIESFILE);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_COOKIEJAR, Enums.COOKIESFILE);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_FOLLOWLOCATION, true);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_POST, true);

            LibCurl.Easy.DebugFunction df = new LibCurl.Easy.DebugFunction(OnDebug);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_DEBUGFUNCTION, df);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_VERBOSE, true);

            LibCurl.Easy.ProgressFunction pf = new LibCurl.Easy.ProgressFunction(OnProgress);
            easy.SetOpt(LibCurl.CURLoption.CURLOPT_PROGRESSFUNCTION, pf);

            wf = new LibCurl.Easy.WriteFunction(OnWriteData);
        }
Exemplo n.º 12
0
 private Easy(Easy from)
 {
     m_pCURL = External.curl_easy_duphandle(from.m_pCURL);
     EnsureHandle();
     m_pMyStrings     = External.curl_shim_alloc_strings();
     m_pfWrite        = null;
     m_privateData    = null;
     m_writeData      = null;
     m_pfRead         = null;
     m_readData       = null;
     m_pfProgress     = null;
     m_progressData   = null;
     m_pfDebug        = null;
     m_debugData      = null;
     m_pfHeader       = null;
     m_headerData     = null;
     m_pfSSLContext   = null;
     m_sslContextData = null;
     m_pfIoctl        = null;
     m_ioctlData      = null;
     InstallDelegates();
 }
Exemplo n.º 13
0
        // called by libcurlshim
        private static int WriteDelegate(IntPtr buf, int sz, int nmemb,
                                         IntPtr parm)
        {
            int bytes = sz * nmemb;

            byte[] b = new byte[bytes];
            for (int i = 0; i < bytes; i++)
            {
                b[i] = Marshal.ReadByte(buf, i);
            }
            GCHandle gch  = (GCHandle)parm;
            Easy     easy = (Easy)gch.Target;

            if (easy == null)
            {
                return(0);
            }
            if (easy.m_pfWrite == null)
            {
                return(bytes);   // keep going
            }
            return(easy.m_pfWrite(b, sz, nmemb, easy.m_writeData));
        }
Exemplo n.º 14
0
        private static int HeaderDelegate(IntPtr buf, int sz, int nmemb,
                                          IntPtr parm)
        {
            int bytes = sz * nmemb;

            byte[] b = new byte[bytes];
            for (int i = 0; i < bytes; i++)
            {
                b[i] = Marshal.ReadByte(buf, i);
            }
            //GCHandle gch = (GCHandle)parm;
            //Easy easy = (Easy)gch.Target;
            Easy easy = _CurrentEasy;

            if (easy == null)
            {
                return(0);
            }
            if (easy.m_pfHeader == null)
            {
                return(bytes);   // keep going
            }
            return(easy.m_pfHeader(b, sz, nmemb, easy.m_headerData));
        }
Exemplo n.º 15
0
 public static extern short curl_easy_setopt(IntPtr handle, int option, Easy.WriteFunction wf);
Exemplo n.º 16
0
		internal MultiInfo(CURLMSG msg, Easy easy, CURLcode result)
		{
            m_msg = msg;
            m_easy = easy;
            m_result = result;
		}
Exemplo n.º 17
0
        //currently used in: wolfram, steam
        private void sendRequest(string waurl, Easy.WriteFunction wf, string upload)
        {
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
            Easy easy = new Easy();

            Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
            easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);
            //easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            easy.SetOpt(CURLoption.CURLOPT_URL, waurl);
            if(upload != "")
                easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, upload);

            easy.Perform();
            easy.Cleanup();
            Curl.GlobalCleanup();
        }
Exemplo n.º 18
0
 internal MultiInfo(CURLMSG msg, Easy easy, CURLcode result)
 {
     m_msg    = msg;
     m_easy   = easy;
     m_result = result;
 }
Exemplo n.º 19
0
        public static bool gomultipartpost(string url, string user, string password, string fname, string fpath, int ticketid,int maxwaitsec, bool showprogress, TradeLink.API.DebugDelegate deb, out string result)
        {
            debs = deb;
            Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);

            Easy easy = new Easy();
            rresult = new StringBuilder();
            hasresult = false;

            MultiPartForm mf = new MultiPartForm();


            // <input name="frmFileOrigPath">
            mf.AddSection(CURLformoption.CURLFORM_COPYNAME, "document[name]",
                CURLformoption.CURLFORM_COPYCONTENTS, fname,
                CURLformoption.CURLFORM_END);



            // <input type="File" name="f1">
            mf.AddSection(CURLformoption.CURLFORM_COPYNAME, "document[file]",
                CURLformoption.CURLFORM_FILE, fpath,
                CURLformoption.CURLFORM_CONTENTTYPE, "application/octet-stream",
                CURLformoption.CURLFORM_END);

            // <input name="frmFileDate">
            if (ticketid != 0)
                mf.AddSection(CURLformoption.CURLFORM_COPYNAME, "document[ticket_id]",
                    CURLformoption.CURLFORM_COPYCONTENTS, ticketid.ToString(),
                    CURLformoption.CURLFORM_END);

            if (showprogress)
            {
                Easy.ProgressFunction pf = new Easy.ProgressFunction(OnProgress);
                easy.SetOpt(CURLoption.CURLOPT_PROGRESSFUNCTION, pf);
            }


            Easy.WriteFunction wf = new Easy.WriteFunction(OnWritePostData);
            easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
            easy.SetOpt(CURLoption.CURLOPT_HTTPPOST, mf);
            Easy.DebugFunction df = new Easy.DebugFunction(OnDebug);
            easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df);

            // simple post - with a string
            //easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS,data);
            Slist sl = new Slist();
            //sl.Append("Content-Type:multipart/form-data");
            sl.Append("Accept: application/xml");
            easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, sl);
            easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false);
            easy.SetOpt(CURLoption.CURLOPT_USERAGENT,
                "Mozilla 4.0 (compatible; MSIE 6.0; Win32");
            easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true);
            easy.SetOpt(CURLoption.CURLOPT_USERPWD, user + ":" + password);
            CURLhttpAuth authflag = CURLhttpAuth.CURLAUTH_BASIC;
            easy.SetOpt(CURLoption.CURLOPT_HTTPAUTH, authflag);
            easy.SetOpt(CURLoption.CURLOPT_URL, url);

            //easy.SetOpt(CURLoption.CURLOPT_POST, true);
            if (debs != null)
                easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true);



            CURLcode err = easy.Perform();
            int waits = 0;
            int maxwaits = 100*maxwaitsec;
            while (!hasresult && (waits++ < maxwaits))
                System.Threading.Thread.Sleep(10);

            int rcodei = 0;
            CURLcode rcode = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref rcodei);


            if (!hasresult && (deb != null))
                deb(easy.StrError(err));

            easy.Cleanup();
            mf.Free();


            Curl.GlobalCleanup();
            result = rresult.ToString();



            return hasresult && (rcodei==201);
        }
Exemplo n.º 20
0
        public string GetPostPage(string url, string postData, string proxyPort, string userPassword, bool deleteCookies = true)
        {
            lock(thisLock){

                if(deleteCookies)
                    DeleteCookies();

                Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
                Easy easy = new Easy();
                try
                {
                    this.CurrentPage = string.Empty;
                    Easy.WriteFunction wf = this.WriteData;
                    easy.SetOpt(CURLoption.CURLOPT_URL, url);
                    easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0);
                    easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0);
                    if (!string.IsNullOrEmpty(proxyPort)) easy.SetOpt(CURLoption.CURLOPT_PROXY, proxyPort);
                    if (!string.IsNullOrEmpty(userPassword)) easy.SetOpt(CURLoption.CURLOPT_PROXYUSERPWD, userPassword);
                    if (!string.IsNullOrEmpty(proxyPort)) easy.SetOpt(CURLoption.CURLOPT_PROXYTYPE, CURLproxyType.CURLPROXY_SOCKS5);
                    easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent);
                    easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, "injector.cookie");
                    easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, "injector.cookie");
                    easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, 1);
                    easy.SetOpt(CURLoption.CURLOPT_AUTOREFERER, 1);
                    easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, 15);
                    if (!string.IsNullOrEmpty(postData)) easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, postData);
                    easy.Perform();
                    return this.CurrentPage;

                }
                catch (Exception exp){
                    if(exp is ThreadAbortException){
                        this.CurrentPage = string.Empty;
                    }
                    Cache.Instance.Log("[GetPostPage] Exception " + exp.ToString());
                }
                finally
                {
                    if (easy != null)
                        easy.Cleanup();
                    Curl.GlobalCleanup();
                }
                return this.CurrentPage = string.Empty;
            }
        }
Exemplo n.º 21
0
 private Easy(Easy from)
 {
     m_pCURL = External.curl_easy_duphandle(from.m_pCURL);
     EnsureHandle();
     m_pMyStrings     = External.curl_shim_alloc_strings();
     m_pfWrite        = null;
     m_privateData    = null;
     m_writeData      = null;
     m_pfRead         = null;
     m_readData       = null;
     m_pfProgress     = null;
     m_progressData   = null;
     m_pfDebug        = null;
     m_debugData      = null;
     m_pfHeader       = null;
     m_headerData     = null;
     m_pfSSLContext   = null;
     m_sslContextData = null;
     m_pfIoctl        = null;
     m_ioctlData      = null;
     InstallDelegates();
 }
Exemplo n.º 22
0
 /// <summary>
 /// Remove an Easy object.
 /// </summary>
 /// <param name="easy">
 /// <see cref="Easy"/> object to remove.
 /// </param>
 /// <returns>
 /// A <see cref="CURLMcode"/>, hopefully <c>CURLMcode.CURLM_OK</c>
 /// </returns>
 /// <exception cref="System.NullReferenceException">
 /// This is thrown if the native <c>Multi</c> handle wasn't
 /// created successfully.
 /// </exception>
 public CURLMcode RemoveHandle(Easy easy)
 {
     EnsureHandle();
     IntPtr p = easy.GetHandle();
     m_htEasy.Remove(p);
     return External.curl_multi_remove_handle(m_pMulti,
         easy.GetHandle());
 }
Exemplo n.º 23
0
 private static Easy _CurrentEasy = null;           //设置当前使用的easy,因为GCHandle在Mono下不可用
 public static void SetCurrentEasy(Easy current)
 {
     _CurrentEasy = current;
 }
Exemplo n.º 24
0
 /// <summary>
 /// Add an Easy object.
 /// </summary>
 /// <param name="easy">
 /// <see cref="Easy"/> object to add.
 /// </param>
 /// <returns>
 /// A <see cref="CURLMcode"/>, hopefully <c>CURLMcode.CURLM_OK</c>
 /// </returns>
 /// <exception cref="System.NullReferenceException">
 /// This is thrown if the native <c>Multi</c> handle wasn't
 /// created successfully.
 /// </exception>
 public CURLMcode AddHandle(Easy easy)
 {
     EnsureHandle();
     IntPtr p = easy.GetHandle();
     m_htEasy.Add(p, easy);
     return External.curl_multi_add_handle(m_pMulti,
         easy.GetHandle());
 }