Example #1
0
        /// <summary>
        /// Prepares multipart request data (postdata and files to upload) and
        /// outputs postdata collection to request stream.
        /// </summary>
        public static string AddMultipartRequestData(this HttpWebRequest request, PostDataCollection postData,
                                                     IFileSystem fileSystem)
        {
            // get ususal postdata and files
            List <PostDataField> textPostData = postData.FindAll(f => f.Type != PostDataFieldType.File);
            List <PostDataField> filePostData = postData.FindAll(f => f.Type == PostDataFieldType.File);

            NameValueCollection filesToUpload = ParseFilePostData(filePostData);

            // set corresponding content type
            request.ContentType = "multipart/form-data; boundary=" + _boundary;

            // stream where we going to combine postdata and files to upload
            Stream memoryStream = new MemoryStream();

            byte[] result = null;

            try
            {
                // add postdata
                foreach (PostDataField field in textPostData)
                {
                    WriteFieldToMemoryStream(memoryStream, field.Name, field.Value);
                }

                // write boundary after every form element's value
                memoryStream.Write(_boundaryBytes, 0, _boundaryBytes.Length);

                // add files
                foreach (string key in filesToUpload)
                {
                    WriteFileToMemoryStream(memoryStream, key, filesToUpload[key], fileSystem);
                }

                // now read everything from memory stream to buffer
                memoryStream.Position = 0;
                result = new byte[memoryStream.Length];
                memoryStream.Read(result, 0, result.Length);

                // set correct content length
                request.ContentLength = result.Length;

                // write buffer to request stream
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(result, 0, result.Length);
                requestStream.Close();
            }
            finally
            {
                memoryStream.Close();
            }

            return(Encoding.UTF8.GetString(result));
        }
Example #2
0
        public void IfFormContainsDifferentInputElements_PostDataCollection_ShouldBeAbleToFilter()
        {
            //Arrange
            string html = @"
                <html>
                        <body>
                            <form id='form1' action='action' method='put'>
                                <input type='text' name='textbox1' value='textvalue' />
                                <input type='password' name='password1' value='passvalue' />
                                <input type='checkbox' name='checkbox1' value='checkvalue' />
                                <input type='radio' name='radio1' value='radiovalue' />
                                <input type='reset' name='reset1' value='resetvalue' />
                                <input type='file' name='file1' value='filevalue' />
                                <input type='file' name='file2' value='filevalue2' />
                            </form>
                        </body>
                    </html>";

            HtmlFormElement form = (HtmlFormElement)HtmlElement.Create(html).ChildElements.Find("form1");

            //Act
            PostDataCollection postData = form.GetPostDataCollection();

            // Assert
            MSAssert.AreEqual("textbox1=textvalue&password1=passvalue", postData.GetPostDataString(PostDataFieldType.Text));
            MSAssert.AreEqual(4, postData.Count);
            MSAssert.AreEqual(2, postData.FindAll(e => (e.Type == PostDataFieldType.File)).Count());
        }