출처 : https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
private void btn_uploadtest_Click(object sender, EventArgs e)
{
string token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGhzIjpbeyJhdXRob3JpdHkiOiJST0xFX1VTRVIifSx7ImF1dGhvcml0eSI6IlJPTEVfQURNSU4ifV0sImV4cCI6MTYwMzAxMzI0OSwiaWF0IjoxNjAyNDA4NDQ5fQ.FfUMOd8NIFlUBCE6aZ1zZvR_IHK6MKiCA6lGfqL3yCZRIGS02c7IY9oBWt824_RAPjSZjbsX3-T3MYIH5C6m1g";
string msg;
NameValueCollection nvc = new NameValueCollection();
nvc.Add("contentId", "1");
HttpUploadFile("http://localhost:9999/admin/file", @"D:\util\mLauncher_bin.zip", "file", "application/octet-stream", token, nvc, out msg);
Console.WriteLine(msg);
}
public static bool HttpUploadFile(string url, string file, string paramName, string contentType, string accessToken, NameValueCollection nvc, out string Msg)
{
bool _flag = false;
Msg = null;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.PreAuthenticate = true;
wr.Headers.Add("Authorization", "Bearer " + accessToken);
wr.Accept = "application/json";
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, Path.GetFileName(file), contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
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)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
using (HttpWebResponse resp = wr.GetResponse() as HttpWebResponse)
{
if (resp.StatusCode == HttpStatusCode.OK)
{
_flag = true;
}
StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
Msg = reader.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return _flag;
}