Skip to content

Instantly share code, notes, and snippets.

@mauricio
Created January 25, 2012 15:24
Show Gist options
  • Save mauricio/1676771 to your computer and use it in GitHub Desktop.
Save mauricio/1676771 to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Collections.Generic;
namespace Core.Api.Http
{
public class Credential {
public String Username { get; set; }
public String Password { get; set; }
}
public class HttpHelper
{
public static HttpResult HttpUploadFile (Credential credential, string url, string file, string paramName, string contentType, IDictionary<string,string> parameters)
{
string boundary = "my-drop-file-upload" + DateTime.Now.Ticks.ToString ("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes ( "--" + boundary);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create (url);
httpRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpRequest.Method = "POST";
httpRequest.KeepAlive = true;
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
byte[] base64Data = System.Text.Encoding.ASCII.GetBytes (String.Format ("{0}:{1}", credential.Username, credential.Password));
string basicHeader = String.Format ("Basic {0}", System.Convert.ToBase64String (base64Data));
httpRequest.Headers.Set ("Authorization", basicHeader);
FillStream (httpRequest.GetRequestStream(), boundarybytes, parameters, file, paramName, contentType);
HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse ();
using (Stream responseStream = response.GetResponseStream()) {
using (StreamReader responseReader = new StreamReader( responseStream )) {
return new HttpResult (response, responseReader.ReadToEnd());
}
}
}
public static void FillStream (Stream requestStream, byte[] boundaryBytes, IDictionary<string,string> parameters, string file, string fileParameterName, string contentType)
{
byte[] newLine = System.Text.Encoding.ASCII.GetBytes ("\r\n");
using (requestStream) {
requestStream.Write (boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write (newLine, 0, newLine.Length);
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (KeyValuePair<string,string> pair in parameters) {
string formitem = string.Format (formdataTemplate, pair.Key, pair.Value);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes (formitem);
requestStream.Write (formitembytes, 0, formitembytes.Length);
requestStream.Write (newLine, 0, newLine.Length);
requestStream.Write (boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write (newLine, 0, newLine.Length);
}
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format (headerTemplate, fileParameterName, Path.GetFileName (file), contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes (header);
requestStream.Write (headerbytes, 0, headerbytes.Length);
using (FileStream fileStream = new FileStream (file, FileMode.Open, FileAccess.Read)) {
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
requestStream.Write (buffer, 0, bytesRead);
}
}
requestStream.Write (newLine, 0, newLine.Length);
requestStream.Write (boundaryBytes, 0, boundaryBytes.Length);
byte[] dashes = System.Text.Encoding.ASCII.GetBytes ("--");
requestStream.Write (dashes, 0, dashes.Length);
requestStream.Write (newLine, 0, newLine.Length);
requestStream.Flush ();
}
}
}
}
@franksandersen
Copy link

franksandersen commented Jun 22, 2018

HttpResult ? Is it missing here? A NuGet package ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment