利用HttpWebRequest模拟提交图片

最近在做排量post工具, 以前做的都是提交文字 这次需要post图片过去,弄了半天终于弄好了;

 /// <summary>
/// Post发送图片数据
/// </summary>
public Queusresult requestData(string uri, string imgUrl, List<dataModes> ListMode, Encoding en, CookieContainer cook)
{
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/octet-stream";
     //一些不需要的请求头 如果去掉不影响久尽量去掉
// req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.CookieContainer = cook;
req.AllowAutoRedirect = true;

     //可能post了其他参数需要跟图片拼接,我这里没有 所以注释了
//StringBuilder postData = new StringBuilder("");
//foreach (dataModes m in ListMode)
//{
// postData.AppendFormat("{0}={1}&", m.keys, m.values);
//}
//postData.AppendFormat("{0}={1}", "1", "1");
//Byte[] writData = ASCIIEncoding.ASCII.GetBytes(postData.ToString()); //读取图片
FileStream oStream = new FileStream(imgUrl, FileMode.Open, FileAccess.Read);
BinaryReader oReader = new BinaryReader(oStream);
Byte[] imgData = oReader.ReadBytes(Convert.ToInt32(oStream.Length)); //byte[] data3 = new byte[writData.Length + imgData.Length];
//System.Array.Copy(writData, 0, data3, 0, writData.Length);
//System.Array.Copy(imgData, 0, data3, writData.Length, imgData.Length); req.ContentLength = imgData.Length;
Stream stream = req.GetRequestStream();
stream.Write(imgData, 0, imgData.Length);
stream.Close(); HttpWebResponse response = req.GetResponse() as HttpWebResponse;
Stream str = response.GetResponseStream();
string html = "";
using (StreamReader strRead = new StreamReader(str, en))
{
req.CookieContainer.Add(response.Cookies);
html = strRead.ReadToEnd();
}
Queusresult q = new Queusresult();
q.html = html;
q.cookie = cook;
return q;

测试成功,如果有不懂的可以加加群交流 .Net技术交流区  86594082

最新文章

  1. 多种JSON格式及遍历方式
  2. H5中的拖拽事件
  3. 编译原理简单语法分析器(first,follow,分析表)源码下载
  4. 轻松自动化---selenium-webdriver(python) (七)
  5. Log4Net 配置SQL2008数据库 并传入自定义业务对象
  6. [转载]线程间操作无效: 从不是创建控件“ListBox1”的线程访问它
  7. 获取html上元素的真正坐标
  8. 优化mysql数据库的几个步骤
  9. 在 JavaScript 中使用构造器函数模拟类
  10. T-SQL 删除重复数据SQL
  11. PHP 5 Calendar 函数
  12. C#开发命名规范总结整理
  13. LeetCode 70 - 爬楼梯 - [递推+滚动优化]
  14. 在java中使用ssm框架的定时的实现
  15. javascript之奇淫技巧
  16. LIN 笔记
  17. Visual自动添加CSS兼容前缀
  18. CS131&amp;Cousera图像处理学习笔记 - L5边缘
  19. 2018.10.26 NOIP模拟 图(最小生成树+线段树合并)
  20. ubuntu16.04 安装 nginx 服务器

热门文章

  1. 如何将Canvas中内容保存为图片
  2. spring其他配置 (3)
  3. Ubuntu 18.04.2 aliases 设置永久生效解决方案
  4. Mysql 插入数据,随机事件选择
  5. js用document.getElementById时要注意!
  6. de4Dot用法 解决 .net程序 reflecter反编译 “索引超出了数组界限”问题
  7. 13-Ubuntu-查阅终端命令版本信息和帮助信息
  8. MZ头里面的东西。真他妈多
  9. go 函数和流程控制
  10. Impala简介