public string HttpGet(string url, bool xml = false) { url = url.Replace(" ", "%20"); // url = SeasideResearch.LibCurlNet.Curl.Escape(url, url.Length); page = ""; easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, null); if (xml) { Slist headers = new Slist(); headers.Append("Accept: application/json, text/javascript, */*"); headers.Append("X_REQUESTED_WITH: XMLHttpRequest"); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers); } easy.SetOpt(CURLoption.CURLOPT_HTTPGET, true); //easy.SetOpt(CURLoption.CURLOPT_REFERER, CurrentURL); easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true); easy.Perform(); easy.GetInfo(CURLINFO.CURLINFO_EFFECTIVE_URL, ref currentURL); if (this.page.Contains("<!DOCTYPE")) { doc.LoadHtml(this.page); } return(this.page); }
public static Int64 GetUrlFileSizeEx(string url, string hostname, int timeout) { long size = -1; Easy easy = new Easy(); Easy.SetCurrentEasy(easy); Slist headers = new Slist(); headers.Append(HobaText.Format("Host: {0}", hostname)); easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L); easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers); //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L); int error = 0; double downloadFileLength = 0.0f; if (easy.Perform() == CURLcode.CURLE_OK) { CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error); if (code == CURLcode.CURLE_OK && error == 200) { easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref downloadFileLength); } if (downloadFileLength >= 0.0f) { size = (int)downloadFileLength; } } else { size = -1; } headers.FreeAll(); Easy.SetCurrentEasy(null); easy.Cleanup(); return(size); }
public static string GetUrlContentType(string url, string hostname, int timeout) { Easy easy = new Easy(); Easy.SetCurrentEasy(easy); Slist headers = new Slist(); headers.Append(HobaText.Format("Host: {0}", hostname)); easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L); easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers); //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L); int error = 0; string contentType = ""; if (easy.Perform() == CURLcode.CURLE_OK) { CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error); if (code == CURLcode.CURLE_OK && error == 200) { easy.GetInfo(CURLINFO.CURLINFO_CONTENT_TYPE, ref contentType); } } headers.FreeAll(); Easy.SetCurrentEasy(null); easy.Cleanup(); return(contentType); }
public MemoryStream LoadBinary(string uri, string method = "GET", string postData = "", List <string> headers = null) { ms = new MemoryStream(); Easy easy = new Easy(); Easy.WriteFunction wf = MyWriteBinaryFunction; easy.SetOpt(CURLoption.CURLOPT_URL, uri); easy.SetOpt(CURLoption.CURLOPT_HEADER, false); easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true); Slist headerSlist = new Slist(); if (headers != null) { foreach (var header in headers) { headerSlist.Append(header); } } easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headerSlist); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false); easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent); easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, 10); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, 3); if (!string.IsNullOrEmpty(postData)) { easy.SetOpt(CURLoption.CURLOPT_POST, true); easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, postData); } easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile); easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); easy.Perform(); int code = 0; easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref code); easy.Cleanup(); return(ms); }
/// <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); }
public static void run(String[] args) { try { Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); Easy easy = new Easy(); easy.SetOpt(CURLoption.CURLOPT_URL, args[0]); easy.SetOpt(CURLoption.CURLOPT_PRIVATE, "Private string"); easy.SetOpt(CURLoption.CURLOPT_FILETIME, true); easy.Perform(); // now, exercise the various GetInfo stuff double d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_CONNECT_TIME, ref d); Console.WriteLine("Connect Time: {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref d); Console.WriteLine("Content Length (Download): {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_UPLOAD, ref d); Console.WriteLine("Content Length (Upload): {0}", d); string s = null; easy.GetInfo(CURLINFO.CURLINFO_CONTENT_TYPE, ref s); Console.WriteLine("Content Type: {0}", s); DateTime dt = new DateTime(0); easy.GetInfo(CURLINFO.CURLINFO_FILETIME, ref dt); Console.WriteLine("File time: {0}", dt); int n = 0; easy.GetInfo(CURLINFO.CURLINFO_HEADER_SIZE, ref n); Console.WriteLine("Header Size: {0}", n); n = 0; easy.GetInfo(CURLINFO.CURLINFO_HTTPAUTH_AVAIL, ref n); Console.WriteLine("Authentication Bitmask: {0}", n); n = 0; easy.GetInfo(CURLINFO.CURLINFO_HTTP_CONNECTCODE, ref n); Console.WriteLine("HTTP Connect Code: {0}", n); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_NAMELOOKUP_TIME, ref d); Console.WriteLine("Name Lookup Time: {0}", d); n = 0; easy.GetInfo(CURLINFO.CURLINFO_OS_ERRNO, ref n); Console.WriteLine("OS Errno: {0}", n); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_PRETRANSFER_TIME, ref d); Console.WriteLine("Pretransfer time: {0}", d); object o = null; easy.GetInfo(CURLINFO.CURLINFO_PRIVATE, ref o); Console.WriteLine("Private Data: {0}", o); n = 0; easy.GetInfo(CURLINFO.CURLINFO_PROXYAUTH_AVAIL, ref n); Console.WriteLine("Proxy Authentication Schemes: {0}", n); n = 0; easy.GetInfo(CURLINFO.CURLINFO_REDIRECT_COUNT, ref n); Console.WriteLine("Redirect count: {0}", n); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_REDIRECT_TIME, ref d); Console.WriteLine("Redirect time: {0}", d); n = 0; easy.GetInfo(CURLINFO.CURLINFO_REQUEST_SIZE, ref n); Console.WriteLine("Request size: {0}", n); n = 0; easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref n); Console.WriteLine("Response code: {0}", n); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_SIZE_DOWNLOAD, ref d); Console.WriteLine("Download size: {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_SIZE_UPLOAD, ref d); Console.WriteLine("Upload size: {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_SPEED_DOWNLOAD, ref d); Console.WriteLine("Download speed: {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_SPEED_UPLOAD, ref d); Console.WriteLine("Upload speed: {0}", d); n = 0; easy.GetInfo(CURLINFO.CURLINFO_SSL_VERIFYRESULT, ref n); Console.WriteLine("SSL verification result: {0}", n); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_STARTTRANSFER_TIME, ref d); Console.WriteLine("Start transfer time: {0}", d); d = 0.0; easy.GetInfo(CURLINFO.CURLINFO_TOTAL_TIME, ref d); Console.WriteLine("Total time: {0}", d); //easy.Cleanup(); Curl.GlobalCleanup(); } catch (Exception ex) { Console.WriteLine(ex); } }
public bool DoDownload(string url, string hostname, string destFileName, int timeout, long bufferSize, WRITE_DATA_DELEGATE onWriteData, Object extraData) { bool ret = false; Easy easy = new Easy(); Easy.SetCurrentEasy(easy); Slist headers = new Slist(); Easy.WriteFunction wf = new Easy.WriteFunction(onWriteData); CURLcode curlCode = CURLcode.CURLE_OK; try { headers.Append(HobaText.Format("Host: {0}", hostname)); easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, extraData); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); easy.SetOpt(CURLoption.CURLOPT_HEADER, 0L); easy.SetOpt(CURLoption.CURLOPT_VERBOSE, 0L); easy.SetOpt(CURLoption.CURLOPT_BUFFERSIZE, (long)bufferSize); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers); //easy.SetOpt(CURLoption.CURLOPT_IPRESOLVE, (long)CURLipResolve.CURL_IPRESOLVE_V4); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000); // easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L); if (StartPoint > 0) { easy.SetOpt(CURLoption.CURLOPT_RESUME_FROM, StartPoint); } int error = 0; curlCode = easy.Perform(); if (curlCode == CURLcode.CURLE_OK) { CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error); if (code == CURLcode.CURLE_OK && error == 200) { ret = true; } } } finally { headers.FreeAll(); Easy.SetCurrentEasy(null); easy.Cleanup(); } if (curlCode != CURLcode.CURLE_OK) { throw new WebException(String.Format( "Error downloading \"{0}\": {1}", url, curlCode)); } return(ret); }
public long GetFileSize(string url, string hostName, int timeout, ref string modifiedTimeGMT) { long size = -1; modifiedTimeGMT = ""; Easy easy = new Easy(); Easy.SetCurrentEasy(easy); Slist headers = new Slist(); CURLcode curlCode = CURLcode.CURLE_OK; try { headers.Append(HobaText.Format("Host: {0}", hostName)); easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_FILETIME, 1L); easy.SetOpt(CURLoption.CURLOPT_HEADER, 1L); easy.SetOpt(CURLoption.CURLOPT_NOBODY, 1L); easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, timeout / 1000); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0L); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 0L); int error = 0; double downloadFileLength = -1.0f; curlCode = easy.Perform(); if (curlCode == CURLcode.CURLE_OK) { CURLcode code = easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref error); if (code == CURLcode.CURLE_OK && error == 200) { easy.GetInfo(CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref downloadFileLength); DateTime time = new DateTime(); code = easy.GetInfo(CURLINFO.CURLINFO_FILETIME, ref time); if (code == CURLcode.CURLE_OK) { modifiedTimeGMT = time.ToUniversalTime().ToString("R"); } } if (downloadFileLength >= 0.0f) { size = (int)downloadFileLength; } } else { size = -1; } } finally { headers.FreeAll(); Easy.SetCurrentEasy(null); easy.Cleanup(); } if (curlCode != CURLcode.CURLE_OK) { throw new ArgumentException(String.Format( "Error downloading \"{0}\": {1}", url, curlCode)); } return(size); }
public string Load(string uri, string method = "GET", string postData = "", List <string> headers = null) { sb.Clear(); Easy easy = new Easy(); Easy.WriteFunction wf = MyWriteFunction; easy.SetOpt(CURLoption.CURLOPT_URL, uri); easy.SetOpt(CURLoption.CURLOPT_HEADER, DisplayHeaders); easy.SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, FollowRedirects); Slist headerSlist = new Slist(); if (headers != null) { foreach (var header in headers) { headerSlist.Append(header); } } easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headerSlist); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, false); easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, false); easy.SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent); easy.SetOpt(CURLoption.CURLOPT_TIMEOUT, 10); easy.SetOpt(CURLoption.CURLOPT_CONNECTTIMEOUT, 3); if (!string.IsNullOrEmpty(postData)) { easy.SetOpt(CURLoption.CURLOPT_POST, true); easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, postData); } if (method.Equals("POST")) { easy.SetOpt(CURLoption.CURLOPT_POST, true); } easy.SetOpt(CURLoption.CURLOPT_COOKIEFILE, CookieFile); easy.SetOpt(CURLoption.CURLOPT_COOKIEJAR, CookieFile); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); easy.Perform(); int code = 0; easy.GetInfo(CURLINFO.CURLINFO_RESPONSE_CODE, ref code); easy.Cleanup(); //Console.WriteLine(code); if (code == 302) { RedirectUrl = FindString(sb.ToString(), "Location:(.*?)\n"); //Console.WriteLine(RedirectUrl); } string page = sb.ToString(); page = page.Replace("\r\n", ""); // strip all new lines and tabs page = page.Replace("\r", ""); // strip all new lines and tabs page = page.Replace("\n", ""); // strip all new lines and tabs page = page.Replace("\t", ""); // strip all new lines and tabs page = Regex.Replace(page, @">\s+<", "><"); return(page); }
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)); }
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); }