HttpWebRequest 上传图片

时间:2023-03-08 17:51:23
 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);
}

  

   public static string HttpUploadFile(string cookictem, string picFullUrl)
{
string url = "https://xxx.com/survey/upload.do";
string filepath = picFullUrl;
string fileformname = "Section1700782aaf-f6bd-4fbe-900b-47cb61b1bbe6.jpg";
string poststr = "";
// 这个可以是改变的,也可以是下面这个固定的字符串
string boundary = "-----------------------------7e028637305bc"; // 创建request对象
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.ContentType = " multipart/form-data; boundary=---------------------------7e028637305bc";
webrequest.Method = "POST"; CookieContainer myCookieContainer = new CookieContainer();
string[] cookstr = cookictem.Split(';'); foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "xx.com";//必须写对
myCookieContainer.Add(ck);
}
webrequest.CookieContainer = myCookieContainer;
// webrequest.Headers.Add("Cookie: JSESSIONID=8450E28895AF6EE4623155D8C72EC477; shy.sid=18KDOMS6E6FHK625RTN01B4A93BG9VVV; cna=oVVyEFHw734CAdJLEayShOfc; l=AtPTBIaevF7f1zPHa33H5EtBI1n9uGdK; isg=ArCw7w_BnPU0uEDxFJzO9weRiX5r-5RDnjT2GqoBTIveZVEPUgob0iRd69T7; __lang_=zh_CN:UTF-8; B2BCRM_USER_COOKIE=E8FCA7EBACCF51A46F9E23E19AB474CE704977A7F2A993ABE0B5229236C54D1E1015BA71A3B519FCCAEDD85AFE62C95281F1CDB00AF14092A5752A7D0CE017A395D2A570B411648296937B099E55ED7642FAF3845D7DD77BB648D61A8B8EA3CAB7660CB7B53BCB15AF7A8643E18FF5A5F07B5B71B168045343A8BB4FC752003B615A53976D8EED7C820EBE2D2EA70343F14D781F4EE6E5313D47BD751A5799D223DA4F62088A6E4BFDD0475B0CDC80D8BAF361D2207F95B6D6CCBB22EF8091C61052D14AADD7F5F47ACDB1F592FE7D705AD5C6AF5F0D0D219564A482038E3D48B502CC7FC633802E61714293A3A8A342FDEE53C4D874BAF01A5CA5FF139C23120EDE11BA0CEB0F503B9EFA45AD2D1CC930BF289D4427D4BDA7089F0954D5542BFB660FBFE6C859EE5AFD1726D4A6D6A684AF25CFDE21ADE5763262844BCA64FE3F6E176BB93FD2732E4F1F1FB2000A55FCA49C1CC016F3BF0CD0DA8CC719154B7D6A0D423BACF3133CE9A055F80FD5C05BEB884B6BF5BD085B51A652031123763080F0E44CB700FC342991D03523427A8AF8A22889BC5C5B6BF8E039A6BAF8EC8D61B345CAAF46E42737021D87FA4C6530A6ED8392EFC085F848DD991363025D4ED56A3D68C0DEFD27EED9423728A6BD988084578F0C124F7FAF7E554B00B0A1823CB562B2B4A5FD23783003AECA134FD7521A470C74AAFD1A0C139D6A118CD520FCF4548661BD994C8574CBB1CC6F1794932BE5E540DC2BB570B0F6F74D861B795682DB01CCFEFB722B50C227E8B9A2C56C630CAB34066DE5F48D710114EAD67CE1E82A0CC6E3EBEDBC46AADA7C990F2843774B0DA7B08AEA3C572C3E888742D1AA8074C298A95FF4F78FC1FDA78BEABDD76023F6CD37131FCA20C64D8CD805F0252288A138EABBC98762ABDF63086C42F704D5A108863CD69E910BE29C00AE8D9BBE2C31995AE91D905D00F0896C6575AB0421C96F36E9FE586B53EF0F95EB32760E01D93F1580CDF946BAE4C73A0DB0FCDACFBA67B31E978E3976ECD42FB0BB609794FA4ABC13D16114098D1F714838896970A669B17765CCFA9A7E3CDC4B09474C5E25A204B7DD0B35A88DC8B7C39EA510C4A37D90200A831343DC1E381C9540CEA3C38FAEF2445E0A9607AAC2F2D1F2631EFCF6979E66485DF003E55534D8FB58027ECB3740C7EBF6A888FC0FE179E7570DB50F93465B170633A74594A01AB4713345A59F005D46D491D239AED7C355E4331CB65FCB16316E797FE7E4128E5E09E7E36A5D2AB249F555FB6478E70E8788CC0091AF317999F66D6635FE217464FA53BDF88448298C5FC56E4F2992528B74A87543C27F1BFABE5DCC74ABDE2BA0D621B3832D4BA2E1877DCEFA8D61B749BD4D9BF3C8F09936F4D49B92286512E111B8A017CF75D4F6402F536FDB4B241FFE4ADA6D0B6EAC90F9166B2C5ED4611203C636884ECB7CF6995DF5534189ADB5B2DB263FD1706E2CE335CFAF5CB4BE421C5AE5FB85C75DEF1325234B21635128C4F94CE97572C7C342233A128A01A5540E6B8CA39AC0B03AD8A7CD2886A7; __martini_=cDsNpm2idajjElLVotuHYN2KZfumSxEobkIekq/AIzYTFE14Vy1bXAtORAktOEDoXUuoJSOvqk1zM0vlxif2RUgVXCiz8I5+AuRMHyJIeCTzxGa/UWs3iErqbqtNNAYpM3tPwdsT8BJ8hVmUHpoF8PEtCwqlLtnO0hmUudGM6cCrxZH8L+Wb3wecBAP9Zz6hrNPL3D6YXZ+upAy1vIXMedAmauqlkMIV64qcPI/kjLIl+0HZO7r/Mg==; crm_umid=FV8T4hoVe1Fs8UmT8MBw4U4Ve1Fw410sEU8X8ncVEHbl; B2BCRM_SSO_TOKEN=FF0E24D0FC563E23E603590FA6255F40990EA4DF349BF7BADE9E6B5233D334646E14E8D9D88FB19EB9E42BA916404A9E; B2BCRM_LANG=zh-CN; B2BCRM_LAST_HEART_BEAT_TIME=43126D6AED19FF45ABB2C2A0B4F038A4; __martini_0=AhAPwLGenDsxeTWZ4LIOyt%2FQZDdaj6JAGrbXmU3VLrBx%2FXH%2FZx2pGJyTVkY0cDmC9vJdkd%2B46F4xpiyszMrquekVc6MNK9m5vFaGXdVGPFcwYX7SaQ5Vj%2BJ4QLqyLwYFSh9W5zZWhMf5U0JafDyKdHxs3vVwy9kmJ%2Bh1lVKyAKH4H4yPpjz%2FeURx90oQjKr2Z6ziQ48WFakGJcwSfa%2BFM3k%2B6NtThVMVjY7Y%2BW4lcOWaowW040iOTFTKaa49Wao5ASTcdYX69SahEtKl4IAItmND0gK1a1tnCuy%2B9hGd0YS1AqyqtyDoXJX%2FsPqOXqr4oI70Iqa3OqUea2nMstdNzA%3D%3D");
webrequest.Referer = "https://cxxx.com/survey/upload.do?key=AvImageUpload"; //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("-----------------------------7e028637305bc");
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"file0\"; filename=\"D:\\wanglei_file\\Winform_Ali\\CN.SGS.AliDataEntries.App\\bin\\Debug\\Picture\\Section2be0f7475 - 2fc1 - 4dbf - 8860 - 8b35bb567aa2.jpg\"");
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("\r\n");
sb1.Append("-----------------------------7e028637305bc");
sb1.Append("\r\n");
sb1.Append("Content-Disposition: form-data; name=\"key\"");
sb1.Append("\r\n");
sb1.Append("\r\n");
sb1.Append("AvImageUpload");
sb1.Append("\r\n");
sb1.Append("-----------------------------7e028637305bc");
sb1.Append("\r\n");
sb1.Append("Content-Disposition: form-data; name=\"desc\"");
sb1.Append("\r\n");
sb1.Append("\r\n");
sb1.Append("https://xxx.com/survey/survey/questionSet.vm");
sb1.Append("\r\n");
sb1.Append("-----------------------------7e028637305bc--");
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();
}