private XmlResponseMessage GetResponseForUploadMediaItem(string uploadMediaItemUrl, MediaItem mediaItem, QueryParameterList paramaters) { string boundary = "PHOTBUCKET_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss"); HttpWebRequest request = ( HttpWebRequest )HttpWebRequest.Create(uploadMediaItemUrl); request.UserAgent = "Mozilla/4.0 PhotobucketNet API (compatible; MSIE 6.0; Windows NT 5.1)"; request.Method = "POST"; request.KeepAlive = true; request.ContentType = "multipart/form-data; boundary=" + boundary + ""; request.Expect = ""; StringBuilder sb = new StringBuilder(); foreach (QueryParameter paramater in paramaters) { if (paramater.Name == "format") { continue; } else if (paramater.Name == "oauth_signature") { paramater.Value = OAuth.UrlDecode(paramater.Value); } else if (paramater.Name == _description || paramater.Name == _title) { paramater.Value = OAuth.UrlDecode(paramater.Value); } sb.Append("--" + boundary + "\r\n"); sb.Append("Content-Disposition: form-data; name=\"" + paramater.Name + "\"\r\n"); sb.Append("\r\n"); sb.Append(paramater.Value + "\r\n"); } sb.Append("--" + boundary + "\r\n"); sb.Append("Content-Disposition: form-data; name=\"uploadfile\"; filename=\"" + mediaItem.Name + "\"\r\n"); sb.Append("Content-Type: mimetype\r\nContent-Transfer-Ecoding: binary"); sb.Append("\r\n\r\n"); UTF8Encoding encoding = new UTF8Encoding(); byte[] postContents = encoding.GetBytes(sb.ToString()); Stream stream = mediaItem.MediaStream; byte[] photoContents = new byte[stream.Length]; stream.Read(photoContents, 0, photoContents.Length); stream.Close(); byte[] postFooter = encoding.GetBytes("\r\n--" + boundary + "--\r\n"); byte[] dataBuffer = new byte[postContents.Length + photoContents.Length + postFooter.Length]; Buffer.BlockCopy(postContents, 0, dataBuffer, 0, postContents.Length); Buffer.BlockCopy(photoContents, 0, dataBuffer, postContents.Length, photoContents.Length); Buffer.BlockCopy(postFooter, 0, dataBuffer, postContents.Length + photoContents.Length, postFooter.Length); request.ContentLength = dataBuffer.Length; Stream resStream = request.GetRequestStream(); int count = 1; int uploadBit = Math.Max(dataBuffer.Length / 100, 50 * 1024); int uploadSoFar = 0; if (OnUploadMediaProgress != null) { OnUploadMediaProgress(this, new UploadMediaProgressEventArgs(0, dataBuffer.Length)); } for (int i = 0; i < dataBuffer.Length; i = i + uploadBit) { int toUpload = Math.Min(uploadBit, dataBuffer.Length - i); uploadSoFar += toUpload; resStream.Write(dataBuffer, i, toUpload); if ((OnUploadMediaProgress != null) && ((count++) % 5 == 0 || uploadSoFar == dataBuffer.Length)) { OnUploadMediaProgress(this, new UploadMediaProgressEventArgs(uploadSoFar, dataBuffer.Length)); } } resStream.Close(); try { request.Method = _uploadMediaMethod; HttpWebResponse response = ( HttpWebResponse )request.GetResponse(); Stream responseStream = response.GetResponseStream(); return(new XmlResponseMessage(responseStream)); } catch (WebException webEx) { throw PhotobucketException.CreateFromWebException(webEx); } }