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); }
时间: 2024-11-05 21:46:20