Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created August 18, 2015 20:23
Show Gist options
  • Select an option

  • Save christopherbauer/a97efff1d5ab6a7c0ebb to your computer and use it in GitHub Desktop.

Select an option

Save christopherbauer/a97efff1d5ab6a7c0ebb to your computer and use it in GitHub Desktop.
Webrequest to web api (oh god why)
public static string PostImage(byte[] imageBytes)
{
string output;
var boundary = GetBoundary(); //boundaries are placed between every form value and file in a MIME message
var boundaryFormatted = string.Format("--{0}\r\n\r\n", boundary);
var boundaryBytes = Encoding.ASCII.GetBytes(boundaryFormatted); //this is ascii, but later on everything is encoded in UTF8. Why, you ask? I do not know.
var partFormat = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n"; //this format is for form data values, which we are not using
var fileHeaderFormat = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n"; //this format is specifically for files
var request = (HttpWebRequest)WebRequest.Create(EndPointUrl);
request.KeepAlive = false;
request.Method = "POST";
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); //MIME content type header
using (var memoryStream = new MemoryStream())
{
var fileHeader = string.Format(fileHeaderFormat, boundary, "fieldNameHere", "audio-card-3.jpg"); //header formatting
var headerBytes = Encoding.UTF8.GetBytes(fileHeader); //everything in bytes w/ memstream
memoryStream.Write(headerBytes, 0, headerBytes.Length); //write dat
var contentType = "Content-Type: image/jpeg\r\n\r\n"; //jpg content type. at this point we are beginning to encode the image
var contentBytes = Encoding.UTF8.GetBytes(contentType);
memoryStream.Write(contentBytes, 0, contentBytes.Length); //write dat contenttype
memoryStream.Write(imageBytes, 0, imageBytes.Length); //THIS LINE WRITES THE IMAGE BUFFER.
memoryStream.Write(boundaryBytes, 0, boundaryBytes.Length); //write dat boundry
var lastBoundary = string.Format("--{0}--", boundary); //last boundary ends the mime message and compliant consumers will blow up without it
var lastBoundaryBytes = Encoding.ASCII.GetBytes(lastBoundary);
memoryStream.Write(lastBoundaryBytes, 0, lastBoundaryBytes.Length); //write dat
request.ContentLength = memoryStream.Length;
using (var requestStream = request.GetRequestStream())
{
memoryStream.Position = 0; //take the memstream
var memBuffer = new byte[memoryStream.Length];
memoryStream.Read(memBuffer, 0, memBuffer.Length); //read it into membuffer
memoryStream.Close();
requestStream.Write(memBuffer, 0, memBuffer.Length); //write membuffer to request stream
}
}
using (var response = request.GetResponse()) //send the MIME message
{
using (var responseStream = response.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
output = reader.ReadToEnd();
}
}
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment