HttpWebRequest 上传文件
2011-09-13 14:33 LoujaDy 阅读(8139) 评论(1) 编辑 收藏 举报 //边界
string boundary = DateTime.Now.Ticks.ToString("x");
HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(url);//url为上传的地址
uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary;
uploadRequest.Method = "POST";
uploadRequest.Accept = "*/*";
uploadRequest.KeepAlive = true;
uploadRequest.Headers.Add("Accept-Language", "zh-cn");
uploadRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
uploadRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
uploadRequest.Headers["Cookie"] = cookies;//上传文件时需要的cookies
WebResponse reponse;
//创建一个内存流
Stream memStream = new MemoryStream();
//确定上传的文件路径
if (!String.IsNullOrEmpty(file))
{
boundary = "--" + boundary;
//添加上传文件参数格式边界
string paramFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}\r\n";
NameValueCollection param = new NameValueCollection();
param.Add("fname", Guid.NewGuid().ToString() + Path.GetExtension(file));
//写上参数
foreach (string key in param.Keys)
{
string formitem = string.Format(paramFormat, key, param[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
//添加上传文件数据格式边界
string dataFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n";
string header = string.Format(dataFormat, "Filedata", Path.GetFileName(file));
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
//获取文件内容
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
//将文件内容写进内存流
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
//添加文件结束边界
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n\n" + boundary + "\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\nSubmit Query\r\n" + boundary + "--");
memStream.Write(boundarybytes, 0, boundarybytes.Length);
//设置请求长度
uploadRequest.ContentLength = memStream.Length;
//获取请求写入流
Stream requestStream = uploadRequest.GetRequestStream();
//将内存流数据读取位置归零
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
//将内存流中的buffer写入到请求写入流
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
//获取到上传请求的响应
reponse = uploadRequest.GetResponse();
string boundary = DateTime.Now.Ticks.ToString("x");
HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(url);//url为上传的地址
uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary;
uploadRequest.Method = "POST";
uploadRequest.Accept = "*/*";
uploadRequest.KeepAlive = true;
uploadRequest.Headers.Add("Accept-Language", "zh-cn");
uploadRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
uploadRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
uploadRequest.Headers["Cookie"] = cookies;//上传文件时需要的cookies
WebResponse reponse;
//创建一个内存流
Stream memStream = new MemoryStream();
//确定上传的文件路径
if (!String.IsNullOrEmpty(file))
{
boundary = "--" + boundary;
//添加上传文件参数格式边界
string paramFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}\r\n";
NameValueCollection param = new NameValueCollection();
param.Add("fname", Guid.NewGuid().ToString() + Path.GetExtension(file));
//写上参数
foreach (string key in param.Keys)
{
string formitem = string.Format(paramFormat, key, param[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
//添加上传文件数据格式边界
string dataFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type:application/octet-stream\r\n\r\n";
string header = string.Format(dataFormat, "Filedata", Path.GetFileName(file));
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
//获取文件内容
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
//将文件内容写进内存流
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
//添加文件结束边界
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n\n" + boundary + "\r\nContent-Disposition: form-data; name=\"Upload\"\r\n\nSubmit Query\r\n" + boundary + "--");
memStream.Write(boundarybytes, 0, boundarybytes.Length);
//设置请求长度
uploadRequest.ContentLength = memStream.Length;
//获取请求写入流
Stream requestStream = uploadRequest.GetRequestStream();
//将内存流数据读取位置归零
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
//将内存流中的buffer写入到请求写入流
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
//获取到上传请求的响应
reponse = uploadRequest.GetResponse();
作者:Louja
出处:http://loujady.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面给出原文连接,否则保留追究法律责任的权利。