AddSection() public method

Add a multi-part form section.
This is definitely the workhorse method for this class. It should be called in roughly the same manner as curl_formadd(), except you would omit the first two struct curl_httppost** arguments (firstitem and lastitem), which are wrapped in this class. So you should pass arguments in the following sequence:

CurlHttpMultiPartForm.AddSection(option1, value1, ..., optionX, valueX, CurlFormOption.End) ;

For a complete list of possible options, see the documentation for the CurlFormOption enumeration.

The pointer options (PtrName, etc.) make an internal copy of the passed byte array. Therefore, any changes you make to the client copy of this array AFTER calling this method, won't be reflected internally with cURL. The purpose of providing the pointer options is to support the posting of non-string binary data.
public AddSection ( ) : CurlFormCode
return CurlFormCode
Example #1
0
        public static void Main(String[] args)
        {
            try
            {
                Curl.GlobalInit(CurlInitFlag.All);

                // <form action="http://mybox/cgi-bin/myscript.cgi
                //  method="post" enctype="multipart/form-data">
                using (var mf = new CurlHttpMultiPartForm())
                {
                    mf.AddSection(CurlFormOption.CopyName, "frmUsername",
                        CurlFormOption.CopyContents, "testtcc",
                        CurlFormOption.End);

                    // <input name="frmPassword">
                    mf.AddSection(CurlFormOption.CopyName, "frmPassword",
                        CurlFormOption.CopyContents, "tcc",
                        CurlFormOption.End);

                    // <input name="frmFileOrigPath">
                    mf.AddSection(CurlFormOption.CopyName, "frmFileOrigPath",
                        CurlFormOption.CopyContents, args[1],
                        CurlFormOption.End);

                    // <input name="frmFileDate">
                    mf.AddSection(CurlFormOption.CopyName, "frmFileDate",
                        CurlFormOption.CopyContents, "08/01/2004",
                        CurlFormOption.End);

                    // <input type="File" name="f1">
                    mf.AddSection(CurlFormOption.CopyName, "f1",
                        CurlFormOption.File, args[1],
                        CurlFormOption.ContentType, "application/binary",
                        CurlFormOption.End);

                    using (var easy = new CurlEasy())
                    {
                        easy.DebugFunction = OnDebug;
                        easy.Verbose = true;
                        easy.ProgressFunction = OnProgress;
                        easy.Url = args[0];
                        easy.HttpPost = mf;

                        easy.Perform();
                    }
                }

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadLine();
        }
Example #2
0
        private static void Main(string[] args)
        {
            var bits = IntPtr.Size*8;
            Console.WriteLine("Test curl {0} bit", bits);
            Curl.GlobalInit(CurlInitFlag.All);
            Console.WriteLine("Curl Version: {0}\n", Curl.Version);

            const string postData = "parm1=12345&parm2=Hello+world%21";
            var postLength = postData.Length;

            Console.WriteLine("\n========== TEST 1 HttpWebRequest ============");

            var request = (HttpWebRequest) WebRequest.Create(TEST_URL);
            var data = Encoding.ASCII.GetBytes(postData);
            request.UserAgent = "HttpWebRequest";
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response = (HttpWebResponse) request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            Console.WriteLine(responseString);

            try
            {
                Console.WriteLine("\n========== TEST 2 CurlEasy PostFields ============");

                using (var easy = new CurlEasy())
                {
                    easy.WriteFunction = OnWriteData;
                    easy.WriteData = null;
                    easy.PostFields = postData;
                    easy.PostFieldSize = postLength;
                    easy.UserAgent = "CurlEasy PostFields";
                    easy.FollowLocation = true;
                    easy.Url = TEST_URL;
                    easy.Post = true;
                    var code = easy.Perform();
                }

                Console.WriteLine("\n========== TEST 3 CurlEasy HttpPost ============");

                var mf = new CurlHttpMultiPartForm();
                mf.AddSection(CurlFormOption.CopyName, "parm1",
                              CurlFormOption.CopyContents, "12345",
                              CurlFormOption.End);
                mf.AddSection(CurlFormOption.CopyName, "parm2",
                              CurlFormOption.CopyContents, "Hello world!",
                              CurlFormOption.End);
                using (var easy = new CurlEasy())
                {
                    easy.WriteFunction = OnWriteData;
                    easy.WriteData = null;
                    easy.UserAgent = "CurlEasy HttpPost";
                    easy.FollowLocation = true;
                    easy.Url = TEST_URL;
                    easy.HttpPost = mf;
                    var code = easy.Perform();
                }

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

            Console.WriteLine("\nPress <ENTER> to exit...");
            Console.ReadLine();
        }