I'm getting closer and figured out that I have to do a multipart
post. It seems to post the text and the image, but the image is
actually missing when I look online. When I click on the image box
with the "X" in it, it comes back and says "Error: images are not
supported", so I'm not sure what is going on here. I'm looking for
anyone who can look at this updated code and tell me possibly what I
am doing wrong. Here's the new code:
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();