public static string HttpUploadFile(string url, string path)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + ); //请求头部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, , bArr.Length);
fs.Close(); Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, , itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, , postHeaderBytes.Length);
postStream.Write(bArr, , bArr.Length);
postStream.Write(endBoundaryBytes, , endBoundaryBytes.Length);
postStream.Close(); //发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
}

最新文章

  1. (转载)GDI+双缓冲
  2. JavaWeb前端基础复习笔记系列 一
  3. 值得 Web 开发人员收藏的20个 HTML5 实例教程
  4. spring容器初始化执行某个方法
  5. php 判断复选框checkbox是否被选中
  6. $().each() 与 $.each()解析
  7. leetcode Database2 (四)
  8. Codeforces 719 E. Sasha and Array (线段树+矩阵运算)
  9. ios开源项目(各种有用的第三方库)
  10. css两句话搞定漂亮表格样式
  11. ASP.NET中时间的绑定和格式化
  12. ubuntu 下安装Angular2-cli脚手架
  13. alpha-咸鱼冲刺day2-紫仪
  14. WC 2018 题解
  15. android屏蔽系统锁屏的办法
  16. 简单理解php深复制浅复制问题
  17. [Hinton] Neural Networks for Machine Learning - Hopfield Nets and Boltzmann Machine
  18. 深入探讨WPF的ListView控件
  19. xgboost学习与总结
  20. SSH框架整合的其它方式

热门文章

  1. plsql无法连接64位oracle数据库的解决方法
  2. 让memcached分布式
  3. PHP大小写:函数名和类名不区分,变量名区分
  4. java 调用系统外部的某个程序
  5. Python Twisted系列教程7:小插曲,Deferred
  6. Anaconda使用总结(文章来自网络)
  7. 微信小程序wxss设置样式
  8. ASP.NET Core应用到Windows Service中
  9. linux rz -e
  10. OK6410之tftp下载内核,nfs…