来自 C# 客户端的多部分表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/219827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multipart forms from C# client
提问by bash74
I am trying to fill a form in a php application from a C# client (Outlook addin). I used Fiddler to see the original request from within the php application and the form is transmitted as a multipart/form. Unfortunately .Net does not come with native support for this type of forms (WebClient has only a method for uploading a file). Does anybody know a library or has some code to achieve this? I want to post different values and additionally (but only sometimes) a file.
我正在尝试从 C# 客户端(Outlook 插件)填写 php 应用程序中的表单。我使用 Fiddler 从 php 应用程序中查看原始请求,并且表单作为多部分/表单传输。不幸的是,.Net 没有原生支持这种类型的表单(WebClient 只有一种上传文件的方法)。有人知道图书馆或有一些代码来实现这一目标吗?我想发布不同的值,另外(但只是有时)一个文件。
Thanks for your help, Sebastian
感谢您的帮助,塞巴斯蒂安
采纳答案by dnolan
This is cut and pasted from some sample code I wrote, hopefully it should give the basics. It only supports File data and form-data at the moment.
这是从我写的一些示例代码中剪切和粘贴的,希望它应该提供基础知识。目前只支持文件数据和表单数据。
public class PostData
{
private List<PostDataParam> m_Params;
public List<PostDataParam> Params
{
get { return m_Params; }
set { m_Params = value; }
}
public PostData()
{
m_Params = new List<PostDataParam>();
// Add sample param
m_Params.Add(new PostDataParam("email", "MyEmail", PostDataParamType.Field));
}
/// <summary>
/// Returns the parameters array formatted for multi-part/form data
/// </summary>
/// <returns></returns>
public string GetPostData()
{
// Get boundary, default is --AaB03x
string boundary = ConfigurationManager.AppSettings["ContentBoundary"].ToString();
StringBuilder sb = new StringBuilder();
foreach (PostDataParam p in m_Params)
{
sb.AppendLine(boundary);
if (p.Type == PostDataParamType.File)
{
sb.AppendLine(string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", p.Name, p.FileName));
sb.AppendLine("Content-Type: text/plain");
sb.AppendLine();
sb.AppendLine(p.Value);
}
else
{
sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));
sb.AppendLine();
sb.AppendLine(p.Value);
}
}
sb.AppendLine(boundary);
return sb.ToString();
}
}
public enum PostDataParamType
{
Field,
File
}
public class PostDataParam
{
public PostDataParam(string name, string value, PostDataParamType type)
{
Name = name;
Value = value;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
To send the data you then need to:
要发送数据,您需要:
HttpWebRequest oRequest = null;
oRequest = (HttpWebRequest)HttpWebRequest.Create(oURL.URL);
oRequest.ContentType = "multipart/form-data";
oRequest.Method = "POST";
PostData pData = new PostData();
byte[] buffer = encoding.GetBytes(pData.GetPostData());
// Set content length of our data
oRequest.ContentLength = buffer.Length;
// Dump our buffered postdata to the stream, booyah
oStream = oRequest.GetRequestStream();
oStream.Write(buffer, 0, buffer.Length);
oStream.Close();
// get the response
oResponse = (HttpWebResponse)oRequest.GetResponse();
Hope thats clear, i've cut and pasted from a few sources to get that tidier.
希望那很清楚,我已经从几个来源剪切和粘贴,以使内容更整洁。
回答by jumoel
Building on dnolans example, this is the version I could actually get to work (there were some errors with the boundary, encoding wasn't set) :-)
基于 dnlans 示例,这是我实际可以开始工作的版本(边界有一些错误,未设置编码):-)
To send the data:
发送数据:
HttpWebRequest oRequest = null;
oRequest = (HttpWebRequest)HttpWebRequest.Create("http://you.url.here");
oRequest.ContentType = "multipart/form-data; boundary=" + PostData.boundary;
oRequest.Method = "POST";
PostData pData = new PostData();
Encoding encoding = Encoding.UTF8;
Stream oStream = null;
/* ... set the parameters, read files, etc. IE:
pData.Params.Add(new PostDataParam("email", "[email protected]", PostDataParamType.Field));
pData.Params.Add(new PostDataParam("fileupload", "filename.txt", "filecontents" PostDataParamType.File));
*/
byte[] buffer = encoding.GetBytes(pData.GetPostData());
oRequest.ContentLength = buffer.Length;
oStream = oRequest.GetRequestStream();
oStream.Write(buffer, 0, buffer.Length);
oStream.Close();
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
The PostData class should look like:
PostData 类应如下所示:
public class PostData
{
// Change this if you need to, not necessary
public static string boundary = "AaB03x";
private List<PostDataParam> m_Params;
public List<PostDataParam> Params
{
get { return m_Params; }
set { m_Params = value; }
}
public PostData()
{
m_Params = new List<PostDataParam>();
}
/// <summary>
/// Returns the parameters array formatted for multi-part/form data
/// </summary>
/// <returns></returns>
public string GetPostData()
{
StringBuilder sb = new StringBuilder();
foreach (PostDataParam p in m_Params)
{
sb.AppendLine("--" + boundary);
if (p.Type == PostDataParamType.File)
{
sb.AppendLine(string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", p.Name, p.FileName));
sb.AppendLine("Content-Type: application/octet-stream");
sb.AppendLine();
sb.AppendLine(p.Value);
}
else
{
sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));
sb.AppendLine();
sb.AppendLine(p.Value);
}
}
sb.AppendLine("--" + boundary + "--");
return sb.ToString();
}
}
public enum PostDataParamType
{
Field,
File
}
public class PostDataParam
{
public PostDataParam(string name, string value, PostDataParamType type)
{
Name = name;
Value = value;
Type = type;
}
public PostDataParam(string name, string filename, string value, PostDataParamType type)
{
Name = name;
Value = value;
FileName = filename;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
回答by Anthony
I needed to simulate a browser login to a website to get a login cookie, and the login form was multipart/form-data.
我需要模拟浏览器登录网站来获取登录cookie,登录表单是multipart/form-data。
I took some clues from the other answers here, and then tried to get my own scenario working. It took a bit of frustrating trial and error before it worked right, but here is the code:
我从这里的其他答案中获取了一些线索,然后尝试让我自己的场景发挥作用。在它正常工作之前进行了一些令人沮丧的反复试验,但这里是代码:
public static class WebHelpers
{
/// <summary>
/// Post the data as a multipart form
/// </summary>
public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, string> values)
{
string formDataBoundary = "---------------------------" + WebHelpers.RandomHexDigits(12);
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
string formData = WebHelpers.MakeMultipartForm(values, formDataBoundary);
return WebHelpers.PostForm(postUrl, userAgent, contentType, formData);
}
/// <summary>
/// Post a form
/// </summary>
public static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, string formData)
{
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
if (request == null)
{
throw new NullReferenceException("request is not a http request");
}
// Add these, as we're doing a POST
request.Method = "POST";
request.ContentType = contentType;
request.UserAgent = userAgent;
request.CookieContainer = new CookieContainer();
// We need to count how many bytes we're sending.
byte[] postBytes = Encoding.UTF8.GetBytes(formData);
request.ContentLength = postBytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Push it out there
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
}
return request.GetResponse() as HttpWebResponse;
}
/// <summary>
/// Generate random hex digits
/// </summary>
public static string RandomHexDigits(int count)
{
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < count; i++)
{
int digit = random.Next(16);
result.AppendFormat("{0:x}", digit);
}
return result.ToString();
}
/// <summary>
/// Turn the key and value pairs into a multipart form
/// </summary>
private static string MakeMultipartForm(Dictionary<string, string> values, string boundary)
{
StringBuilder sb = new StringBuilder();
foreach (var pair in values)
{
sb.AppendFormat("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", boundary, pair.Key, pair.Value);
}
sb.AppendFormat("--{0}--\r\n", boundary);
return sb.ToString();
}
}
}
It doesn't handle file data, just form since that's all that I needed. I called like this:
它不处理文件数据,只处理表单,因为这就是我所需要的。我是这样打电话的:
try
{
using (HttpWebResponse response = WebHelpers.MultipartFormDataPost(postUrl, UserAgentString, this.loginForm))
{
if (response != null)
{
Cookie loginCookie = response.Cookies["logincookie"];
.....
回答by Brian Grinstead
Thanks for the answers, everybody! I recently had to get this to work, and used your suggestions heavily. However, there were a couple of tricky parts that did not work as expected, mostly having to do with actually including the file (which was an important part of the question). There are a lot of answers here already, but I think this may be useful to someone in the future (I could not find many clear examples of this online). I wrote a blog postthat explains it a little more.
谢谢大家的解答!我最近不得不让它工作,并大量使用你的建议。但是,有几个棘手的部分没有按预期工作,主要与实际包含文件有关(这是问题的重要部分)。这里已经有很多答案了,但我认为这可能对将来的某个人有用(我在网上找不到很多明确的例子)。我写了一篇博文,对它进行了更多的解释。
Basically, I first tried to pass in the file data as a UTF8 encoded string, but I was having problems with encoding files (it worked fine for a plain text file, but when uploading a Word Document, for example, if I tried to save the file that was passed through to the posted form using Request.Files[0].SaveAs(), opening the file in Word did not work properly. I found that if you write the file data directly using a Stream (rather than a StringBuilder), it worked as expected. Also, I made a couple of modifications that made it easier for me to understand.
基本上,我首先尝试将文件数据作为 UTF8 编码的字符串传递,但是我在编码文件时遇到了问题(它对于纯文本文件工作正常,但是在上传 Word 文档时,例如,如果我尝试保存使用 Request.Files[0].SaveAs() 传递到已发布表单的文件,在 Word 中打开文件无法正常工作。我发现如果您直接使用 Stream(而不是 StringBuilder)写入文件数据),它按预期工作。另外,我做了一些修改,使我更容易理解。
By the way, the Multipart Forms Request for Commentsand the W3C Recommendation for mulitpart/form-dataare a couple of useful resources in case anyone needs a reference for the specification.
顺便说一下,Multipart Forms Request for Comments和W3C 对 multipart/form-data 的建议是一些有用的资源,以防任何人需要规范的参考。
I changed the WebHelpers class to be a bit smaller and have simpler interfaces, it is now called FormUpload
. If you pass a FormUpload.FileParameter
you can pass the byte[] contents along with a file name and content type, and if you pass a string, it will treat it as a standard name/value combination.
我将 WebHelpers 类更改为更小且具有更简单的接口,现在称为FormUpload
. 如果您传递 aFormUpload.FileParameter
您可以传递 byte[] 内容以及文件名和内容类型,如果您传递一个字符串,它会将其视为标准名称/值组合。
Here is the FormUpload class:
这是 FormUpload 类:
// Implements multipart/form-data POST in C# http://www.ietf.org/rfc/rfc2388.txt
// http://www.briangrinstead.com/blog/multipart-form-post-in-c
public static class FormUpload
{
private static readonly Encoding encoding = Encoding.UTF8;
public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
{
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
return PostForm(postUrl, userAgent, contentType, formData);
}
private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData)
{
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
if (request == null)
{
throw new NullReferenceException("request is not a http request");
}
// Set up the request properties.
request.Method = "POST";
request.ContentType = contentType;
request.UserAgent = userAgent;
request.CookieContainer = new CookieContainer();
request.ContentLength = formData.Length;
// You could add authentication here as well if needed:
// request.PreAuthenticate = true;
// request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
// request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("username" + ":" + "password")));
// Send the form data to the request.
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(formData, 0, formData.Length);
requestStream.Close();
}
return request.GetResponse() as HttpWebResponse;
}
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
{
Stream formDataStream = new System.IO.MemoryStream();
bool needsCLRF = false;
foreach (var param in postParameters)
{
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
// Skip it on the first parameter, add it to subsequent parameters.
if (needsCLRF)
formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
needsCLRF = true;
if (param.Value is FileParameter)
{
FileParameter fileToUpload = (FileParameter)param.Value;
// Add just the first part of this param, since we will write the file data directly to the Stream
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
param.Key,
fileToUpload.FileName ?? param.Key,
fileToUpload.ContentType ?? "application/octet-stream");
formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
// Write the file data directly to the Stream, rather than serializing it to a string.
formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
}
else
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
boundary,
param.Key,
param.Value);
formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
}
}
// Add the end of the request. Start with a newline
string footer = "\r\n--" + boundary + "--\r\n";
formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));
// Dump the Stream into a byte[]
formDataStream.Position = 0;
byte[] formData = new byte[formDataStream.Length];
formDataStream.Read(formData, 0, formData.Length);
formDataStream.Close();
return formData;
}
public class FileParameter
{
public byte[] File { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public FileParameter(byte[] file) : this(file, null) { }
public FileParameter(byte[] file, string filename) : this(file, filename, null) { }
public FileParameter(byte[] file, string filename, string contenttype)
{
File = file;
FileName = filename;
ContentType = contenttype;
}
}
}
Here is the calling code, which uploads a file and a few normal post parameters:
这是调用代码,它上传一个文件和一些正常的 post 参数:
// Read file data
FileStream fs = new FileStream("c:\people.doc", FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
// Generate post objects
Dictionary<string, object> postParameters = new Dictionary<string, object>();
postParameters.Add("filename", "People.doc");
postParameters.Add("fileformat", "doc");
postParameters.Add("file", new FormUpload.FileParameter(data, "People.doc", "application/msword"));
// Create request and receive response
string postURL = "http://localhost";
string userAgent = "Someone";
HttpWebResponse webResponse = FormUpload.MultipartFormDataPost(postURL, userAgent, postParameters);
// Process response
StreamReader responseReader = new StreamReader(webResponse.GetResponseStream());
string fullResponse = responseReader.ReadToEnd();
webResponse.Close();
Response.Write(fullResponse);
回答by Brian Grinstead
Below is the code which I'm using
下面是我正在使用的代码
//This URL not exist, it's only an example.
string url = "http://myBox.s3.amazonaws.com/";
//Instantiate new CustomWebRequest class
CustomWebRequest wr = new CustomWebRequest(url);
//Set values for parameters
wr.ParamsCollection.Add(new ParamsStruct("key", "${filename}"));
wr.ParamsCollection.Add(new ParamsStruct("acl", "public-read"));
wr.ParamsCollection.Add(new ParamsStruct("success_action_redirect", "http://www.yahoo.com"));
wr.ParamsCollection.Add(new ParamsStruct("x-amz-meta-uuid", "14365123651274"));
wr.ParamsCollection.Add(new ParamsStruct("x-amz-meta-tag", ""));
wr.ParamsCollection.Add(new ParamsStruct("AWSAccessKeyId", "zzzz"));
wr.ParamsCollection.Add(new ParamsStruct("Policy", "adsfadsf"));
wr.ParamsCollection.Add(new ParamsStruct("Signature", "hH6lK6cA="));
//For file type, send the inputstream of selected file
StreamReader sr = new StreamReader(@"file.txt");
wr.ParamsCollection.Add(new ParamsStruct("file", sr, ParamsStruct.ParamType.File, "file.txt"));
wr.PostData();
from the following link I've downloaded the same code http://www.codeproject.com/KB/cs/multipart_request_C_.aspx
从以下链接我下载了相同的代码 http://www.codeproject.com/KB/cs/multipart_request_C_.aspx
Any Help
任何帮助
回答by eeeeaaii
In the version of .NET I am using you also have to do this:
在我使用的 .NET 版本中,您还必须执行以下操作:
System.Net.ServicePointManager.Expect100Continue = false;
If you don't, the HttpWebRequest
class will automatically add the Expect:100-continue
request header which fouls everything up.
如果不这样做,HttpWebRequest
该类将自动添加使Expect:100-continue
所有内容都变得混乱的请求标头。
Also I learned the hard way that you have to have the right number of dashes. whatever you say is the "boundary" in the Content-Type
header has to be preceded by two dashes
我也学到了你必须拥有正确数量的破折号的艰难方法。无论你说什么是Content-Type
标题中的“边界”都必须以两个破折号开头
--THEBOUNDARY
and at the end
最后
--THEBOUNDARY--
exactly as it does in the example code. If your boundary is a lot of dashes followed by a number then this mistake won't be obvious by looking at the http request in a proxy server
与示例代码中的完全一样。如果您的边界是很多破折号后跟一个数字,那么通过查看代理服务器中的 http 请求,这个错误不会很明显
回答by TheQult
A little optimization of the class before. In this version the files are not totally loaded into memory.
之前对类的一点优化。在这个版本中,文件没有完全加载到内存中。
Security advice: a check for the boundary is missing, if the file contains the bounday it will crash.
安全建议:缺少边界检查,如果文件包含边界,它将崩溃。
namespace WindowsFormsApplication1
{
public static class FormUpload
{
private static string NewDataBoundary()
{
Random rnd = new Random();
string formDataBoundary = "";
while (formDataBoundary.Length < 15)
{
formDataBoundary = formDataBoundary + rnd.Next();
}
formDataBoundary = formDataBoundary.Substring(0, 15);
formDataBoundary = "-----------------------------" + formDataBoundary;
return formDataBoundary;
}
public static HttpWebResponse MultipartFormDataPost(string postUrl, IEnumerable<Cookie> cookies, Dictionary<string, string> postParameters)
{
string boundary = NewDataBoundary();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
// Set up the request properties
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.UserAgent = "PhasDocAgent 1.0";
request.CookieContainer = new CookieContainer();
foreach (var cookie in cookies)
{
request.CookieContainer.Add(cookie);
}
#region WRITING STREAM
using (Stream formDataStream = request.GetRequestStream())
{
foreach (var param in postParameters)
{
if (param.Value.StartsWith("file://"))
{
string filepath = param.Value.Substring(7);
// Add just the first part of this param, since we will write the file data directly to the Stream
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
boundary,
param.Key,
Path.GetFileName(filepath) ?? param.Key,
MimeTypes.GetMime(filepath));
formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, header.Length);
// Write the file data directly to the Stream, rather than serializing it to a string.
byte[] buffer = new byte[2048];
FileStream fs = new FileStream(filepath, FileMode.Open);
for (int i = 0; i < fs.Length; )
{
int k = fs.Read(buffer, 0, buffer.Length);
if (k > 0)
{
formDataStream.Write(buffer, 0, k);
}
i = i + k;
}
fs.Close();
}
else
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n",
boundary,
param.Key,
param.Value);
formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, postData.Length);
}
}
// Add the end of the request
byte[] footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
formDataStream.Write(footer, 0, footer.Length);
request.ContentLength = formDataStream.Length;
formDataStream.Close();
}
#endregion
return request.GetResponse() as HttpWebResponse;
}
}
}
回答by Luis Domingues
Thanks for the code, it saved me a lot of time (including the Except100 error!).
感谢您的代码,它为我节省了很多时间(包括 Except100 错误!)。
Anyway, I found a bug in the code, here:
无论如何,我在代码中发现了一个错误,在这里:
formDataStream.Write(encoding.GetBytes(postData), 0, postData.Length);
In case your POST data is utf-16, postData.Length, will return the number of characters and not the number of bytes. This will truncate the data being posted (for example, if you have 2 chars that are encoded as utf-16, they take 4 bytes, but postData.Length will say it takes 2 bytes, and you loose the 2 final bytes of the posted data).
如果您的 POST 数据是 utf-16,则 postData.Length 将返回字符数而不是字节数。这将截断发布的数据(例如,如果您有 2 个编码为 utf-16 的字符,它们占用 4 个字节,但 postData.Length 会说它占用 2 个字节,并且您丢失了已发布的最后 2 个字节数据)。
Solution - replace that line with:
解决方案 - 将该行替换为:
byte[] aPostData=encoding.GetBytes(postData);
formDataStream.Write(aPostData, 0, aPostData.Length);
Using this, the length is calculated by the size of the byte[], not the string size.
使用这个,长度是由字节[]的大小计算的,而不是字符串大小。
回答by Yavanosta
My implementation
我的实现
/// <summary>
/// Sending file via multipart\form-data
/// </summary>
/// <param name="url">URL for send</param>
/// <param name="file">Local file path</param>
/// <param name="paramName">Request file param</param>
/// <param name="contentType">Content-Type file headr</param>
/// <param name="nvc">Additional post params</param>
private static string httpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
//delimeter
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//creating request
var wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
//sending request
using(var requestStream = wr.GetRequestStream())
{
using (var requestWriter = new StreamWriter(requestStream, Encoding.UTF8))
{
//params
const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
requestWriter.Write(boundary);
requestWriter.Write(String.Format(formdataTemplate, key, nvc[key]));
}
requestWriter.Write(boundary);
//file header
const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
requestWriter.Write(String.Format(headerTemplate, paramName, file, contentType));
//file content
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
fileStream.CopyTo(requestStream);
}
requestWriter.Write("\r\n--" + boundary + "--\r\n");
}
}
//reading response
try
{
using (var wresp = (HttpWebResponse)wr.GetResponse())
{
if (wresp.StatusCode == HttpStatusCode.OK)
{
using (var responseStream = wresp.GetResponseStream())
{
if (responseStream == null)
return null;
using (var responseReader = new StreamReader(responseStream))
{
return responseReader.ReadToEnd();
}
}
}
throw new ApplicationException("Error while upload files. Server status code: " + wresp.StatusCode.ToString());
}
}
catch (Exception ex)
{
throw new ApplicationException("Error while uploading file", ex);
}
}
回答by codevision
With .NET 4.5 you currently could use System.Net.Http namespace. Below the example for uploading single file using multipart form data.
使用 .NET 4.5,您目前可以使用 System.Net.Http 命名空间。下面是使用多部分表单数据上传单个文件的示例。
using System;
using System.IO;
using System.Net.Http;
namespace HttpClientTest
{
class Program
{
static void Main(string[] args)
{
var client = new HttpClient();
var content = new MultipartFormDataContent();
content.Add(new StreamContent(File.Open("../../Image1.png", FileMode.Open)), "Image", "Image.png");
content.Add(new StringContent("Place string content here"), "Content-Id in the HTTP");
var result = client.PostAsync("https://hostname/api/Account/UploadAvatar", content);
Console.WriteLine(result.Result.ToString());
}
}
}