masroore / CurlSharp

CurlSharp - .Net binding and object-oriented wrapper for libcurl.
BSD 3-Clause "New" or "Revised" License
182 stars 71 forks source link

curl upload not working when upload this #10

Closed jagadeesh1492 closed 8 years ago

jagadeesh1492 commented 9 years ago

When i post a curl url i was facing this strange issue

message {"Object contains non-primitive or non-blittable data."} at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object value, GCHandleType type) at System.Runtime.InteropServices.GCHandle.Alloc(Object value, GCHandleType type) at CurlSharp.CurlEasy.getHandle(Object obj) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 1526 at CurlSharp.CurlEasy.setReadData(Object data) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 252 at CurlSharp.CurlEasy.set_ReadData(Object value) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\CurlSharp\CurlEasy.cs:line 330 at Upload.Upload.Main(String[] args) in d:\sharedfiles\Jagan\CurlSharp-master\CurlSharp-master\Samples\Upload\Upload.cs:line 36

Curl command is

curl -i -X POST -H "Content-Type: multipart/form-data" -H "Authorization: SIMPLE Y2F0Y2h0aGVtb21lbjpjYXRjaHRoZW1vbWVudDExMQ==" -F "card={\"text\":\"sometext\", \"userName\":\"%USERNAME%\", \"screenName\":\"screenName\", \"avatarUrl\":\"https://www.google.com/images/srpr/logo11w.png\"};type=application/json" -F "pic=@c:\users\my\desktop\download.jpg" "https://socialwall.cvent.com/livestream/api/event/395b4759-8b93-4fb1-b33b-387c3928c1ca/socialpost" -v

below code is my implementation of above curl command

public static void Main(String[] args) { try {

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "download.jpg");
            Curl.GlobalInit(CurlInitFlag.All);

            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                string _cred = string.Format("{0} {1}", "SIMPLE", "Y2F0Y2h0aGVtb21lbjpjYXRjaHRoZW1vbWVudDExMQ==");
                string url = "https://socialwall.cvent.com/livestream/api/event/395b4759-8b93-4fb1-b33b-387c3928c1ca/socialpost";
                var card = new Card
        {
            text = "Ohmygod",
            userName = "CatchTheMoment",
            screenName = "Socialpost",
            avatarUrl = "https://www.google.com/images/srpr/logo11w.png"
        };
        var jsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(card);
                using (var easy = new CurlEasy())
                {
                    easy.ReadFunction = OnReadData;
                    easy.ReadData = fs;
                    var headers = new CurlSlist();
                    headers.Append("Content-Type: multipart/form-data");
                    headers.Append("Accept: */*");
                    headers.Append("user-agent:Mozilla/4.0");
                    headers.Append("Authorization0:"+_cred);
                    headers.Append("Except: 100-continue");

                    easy.HttpHeader = headers;
                    easy.DebugFunction = OnDebug;
                    easy.SetOpt(CurlOption.Verbose, true);
                    easy.SetOpt(CurlOption.Header, true);
                    easy.ProgressFunction = OnProgress;
                    easy.Url = url;
                    easy.Upload = true;
                    easy.InfileSize = fs.Length;
                    easy.PostFields = "card="+jsonstring;
                    easy.PostFieldSize = jsonstring.Length;
                    easy.Perform();
                    Console.ReadLine();
                }
            }

            Curl.GlobalCleanup();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    public static Int32 OnReadData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
    {
        var fs = (FileStream) extraData;
        return fs.Read(buf, 0, size*nmemb);
    }

    public static void OnDebug(CurlInfoType infoType, String msg, Object extraData)
    {
        Console.WriteLine(msg);
    }

    public static Int32 OnProgress(Object extraData, Double dlTotal,
        Double dlNow, Double ulTotal, Double ulNow)
    {
        Console.WriteLine("Progress: {0} {1} {2} {3}",
            dlTotal, dlNow, ulTotal, ulNow);
        return 0; // standard return from PROGRESSFUNCTION
    }
    public class Card
    {

        public string text { get; set; }
        public string userName { get; set; }
        public string screenName { get; set; }
        public string avatarUrl { get; set; }
    }
bapzangbo commented 9 years ago

I have same Problem. have You had solution to fix that ? @jagadeesh1492

Riyas-precise commented 9 years ago

i was moving to another approach by executing curl command in curl exe..its worked now.

Jagadeesh Govindaraj

ryysoft commented 4 years ago

i meet same problem,do me a favour,thanks.