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