HttpWebRequest 上传图片

发布时间:2017-6-25 6:30:00 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"HttpWebRequest 上传图片",主要涉及到HttpWebRequest 上传图片方面的内容,对于HttpWebRequest 上传图片感兴趣的同学可以参考一下。

 public string HttpUploadFile()        {            string url = "http://localhost:50380/WebForm1.aspx";            string filepath = "C:\\Users\\lei2.wang\\Desktop\\Capture.PNG";            string fileformname = "Capture.PNG";            string poststr = "";            // 这个可以是改变的,也可以是下面这个固定的字符串             string boundary = "------WebKitFormBoundarylAiKcuBRGd1pRVJI";            // 创建request对象             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);            webrequest.ContentType = " multipart/form-data; boundary=----WebKitFormBoundarylAiKcuBRGd1pRVJI";            webrequest.Method = "POST";            webrequest.Headers.Add("Cookie: SGSRev_Order_ReportUser=billy-wp_li; SGSBlackClientListUser=seven_jiang; SGSCNComplaintCaseUser=Jiang_Seven; CtsisUserCode=apac\\Seven_Jiang; CNZZDATA1996164=cnzz_eid%3D798484094-1479962914-http%253A%252F%252Flocalhost%253A35708%252F%26ntime%3D1479974059 ");            webrequest.Referer = "http://localhost:50380/WebForm1.aspx";            //webrequest.Proxy = new WebProxy("127.0.0.1", 8888); // new WebProxy("127.0.0.1", "8888");//:8888            // 构造发送数据             StringBuilder sb = new StringBuilder();            // 文本域的数据,将user=eking&pass=123456  格式的文本域拆分 ,然后构造             sb.Append("\r\n");            sb.Append("\r\n");            sb.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"__VIEWSTATE\"");            sb.Append("\r\n");            sb.Append("\r\n");            sb.Append("revyQ+TvGvOWCVIQshKUEnHeGf0ftCH92iHWuRYVAct4lnXANDjVJUFuMPHDleYjIQfcrEsDhwzGYtRqw+fVLeUPs6OhI7/w2gubieoS4RQ=");            sb.Append("\r\n");            // 文件域的数据             sb.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"aaab\"; filename=\"" + fileformname + "\"");            sb.Append("\r\n");            sb.Append("Content-Type: image/jpeg");            sb.Append("\r\n");            sb.Append("\r\n");            string postHeader = sb.ToString();            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);            //构造尾部数据             StringBuilder sb1 = new StringBuilder();            sb1.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb1.Append("\r\n");            sb1.Append("Content-Disposition: form-data; name=\"__VIEWSTATEGENERATOR\"");            sb1.Append("\r\n");            sb1.Append("\r\n");            sb1.Append("C687F31A");            sb1.Append("\r\n");            sb1.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI--");            sb1.Append("\r\n");            sb1.Append("\r\n");            byte[] boundaryBytes = Encoding.ASCII.GetBytes(sb1.ToString());            var cc = sb1.ToString();            FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);            long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;            webrequest.ContentLength = length;            Stream requestStream = webrequest.GetRequestStream();            // 输入头部数据             requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            // 输入文件流数据             byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];            int bytesRead = 0;            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)                requestStream.Write(buffer, 0, bytesRead);            // 输入尾部数据             requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);            WebResponse responce = webrequest.GetResponse();            Stream s = responce.GetResponseStream();            StreamReader sr = new StreamReader(s);            // 返回数据流(源码)             return sr.ReadToEnd();        }

  

 protected void Page_Load(object sender, EventArgs e)        {             var aa=   Request.Files[0];                HttpFileCollection hfc = HttpContext.Current.Request.Files;  //获取文件,保存图片                HttpPostedFile hpf = hfc[0];                int extIndex = hpf.FileName.LastIndexOf('.');                string ext = hpf.FileName.Substring(extIndex);                string fileName = Guid.NewGuid().ToString();                string dir = HttpContext.Current.Server.MapPath("photos/buildings/");                hpf.SaveAs(dir + fileName + ext);        }

上一篇:MySQL 优化数据库对象
下一篇:可重入和不可重入

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。