Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Using HttpWebRequest for a Multipart POST

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JB - 23 Feb 2008 14:22 GMT
Hello,

I am writing a method that performs a HTTP POST and sends two text
parameters and the third is an image that will be uploaded as part of the
HTTP POST.  I can get the function to properly post the text parameters, but
the image shows up as an "X" like it is missing.  It's not missing from the
path, I just believe that there is something wrong with the encoding or how
I am creating the boundary package.  Here is the method; any help would be
appreciated.

  protected string ExecutePostCommand(string filename, byte[] photo, string
text, string place)
   {
       HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(@"http://testdomain.com/api/post");
       request.PreAuthenticate = true;
       request.AllowWriteStreamBuffering = true;

       string boundary = System.Guid.NewGuid().ToString();

       if (!string.IsNullOrEmpty(Username) &&
!string.IsNullOrEmpty(Password))
       {
           request.Credentials = new NetworkCredential(Username, Password);
           request.ContentType = string.Format("multipart/form-data;
boundary={0}", boundary);
           request.Method = "POST";

           // Build Contents for Post
           string header = string.Format("--{0}", boundary);
           string footer = header + "--";

           StringBuilder contents = new StringBuilder();

           // Image
           contents.AppendLine(header);
           contents.AppendLine(string.Format("Content-Disposition:
form-data; name=\"image\"; filename=\"{0}\"", filename));
           contents.AppendLine("Content-Type: image/jpeg");
           contents.AppendLine();
           contents.AppendLine(Encoding.UTF8.GetString(photo));
           // Text
           contents.AppendLine(header);
           contents.AppendLine("Content-Disposition: form-data;
name=\"text\"");
           contents.AppendLine();
           contents.AppendLine(text);
           // Place
           contents.AppendLine(header);
           contents.AppendLine("Content-Disposition: form-data;
name=\"place\"");
           contents.AppendLine();
           contents.AppendLine(place);
           // Footer
           contents.AppendLine(footer);

           // This is sent to the Post
           byte[] bytes = Encoding.UTF8.GetBytes(contents.ToString());

           request.ContentLength = bytes.Length;

           using (Stream requestStream = request.GetRequestStream())
           {
               requestStream.Write(bytes, 0, bytes.Length);
               requestStream.Flush();
               requestStream.Close();

               using (WebResponse response = request.GetResponse())
               {
                   using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
                   {
                       return reader.ReadToEnd();
                   }
               }
           }
       }

       return null;
   }
JackUn - 24 Feb 2008 17:06 GMT
Could be a case of missing Content-Transfer-Encoding: binary
http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2

And http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.headers(VS.71
).aspx

req.Headers.Add("Accept-Language", "cs,en-us;q=0.7,en;q=0.3");
JackUn - 24 Feb 2008 17:09 GMT
Yeah, nevermind the headers part ;)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.