/// <summary>
        /// Internal implementation of the multi-part POST request.
        /// </summary>
        static void PostMultiPartAsync(HttpWebRequest request, IEnumerable <HttpContentItem> contentItems, AsyncCallback callback)
        {
            request.Method = "POST";
            string boundary = "---------------" + DateTime.Now.Ticks.ToString();

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
            {
                Stream stream = request.EndGetRequestStream(asyncResult);

                DataContractMultiPartSerializer ser = new DataContractMultiPartSerializer(boundary);
                ser.WriteContent(stream, contentItems);
                stream.Close();
                request.BeginGetResponse(callback, request);
            }), request);
        }
        /// <summary>
        /// Helper method to perform a multi-part post. If dispatcher is null, the callback occurs on a different thread.
        /// </summary>
        public static void MultiPartPostAsync(Uri uri, IEnumerable <HttpContentItem> contentItems, Dispatcher dispatcher, EventHandler <MultiPartPostEventArgs> callback)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "POST";
            string boundary = "---------------" + DateTime.Now.Ticks.ToString();

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
            {
                Stream stream = request.EndGetRequestStream(asyncResult);

                DataContractMultiPartSerializer ser = new DataContractMultiPartSerializer(boundary);
                ser.WriteContent(stream, contentItems);
                stream.Close();
                request.BeginGetResponse(new AsyncCallback(asyncResult2 =>
                {
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult2);

                    if (dispatcher == null)
                    {
                        callback(null, new MultiPartPostEventArgs()
                        {
                            Result = response.GetResponseStream()
                        });
                        response.Close();
                    }
                    else // switch to the dispatcher thread
                    {
                        dispatcher.BeginInvoke(delegate
                        {
                            callback(null, new MultiPartPostEventArgs()
                            {
                                Result = response.GetResponseStream()
                            });
                            response.Close();
                        });
                    }
                }), request);
            }), request);
        }