C#采取HttpWebRequest完成堅持會話上傳文件到HTTP的辦法。本站提示廣大學習愛好者:(C#采取HttpWebRequest完成堅持會話上傳文件到HTTP的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#采取HttpWebRequest完成堅持會話上傳文件到HTTP的辦法正文
本文實例講述了C#采取HttpWebRequest完成堅持會話上傳文件到HTTP的辦法,在項目開辟中有必定的適用價值,詳細辦法以下:
1、媒介:
這篇文章翻譯來自madmik3 寫在 CodeProject 上的文章,原題目為: C#'s WebClient.UploadFile with more functionality.
2、注釋:
我們應用 WebRequest 來獲得網頁內容長短常簡略的,可是用他來上傳文件就沒有那末簡略了。
假如我們在網頁中上傳文件,參加上面代碼便可:
HTML 文件上傳代碼實例:
<form action ="http://localhost/test.php" method = POST> <input type = text name = uname> <input type = password name =passwd> <input type = FILE name = uploadfile> <input type=submit> </form>
但,假如在C#中應用 WebRequest 上傳,必需對當地文件停止響應的處置能力提交到指定的HTTP地址,上面這個函數哦就幫我們做了這懊惱的操作
UploadFileEx 上傳文件函數:
public static string UploadFileEx( string uploadfile, string url, string fileFormName, string contenttype,NameValueCollection querystring, CookieContainer cookies) { if( (fileFormName== null) || (fileFormName.Length ==0)) { fileFormName = "file"; } if( (contenttype== null) || (contenttype.Length ==0)) { contenttype = "application/octet-stream"; } string postdata; postdata = "?"; if (querystring!=null) { foreach(string key in querystring.Keys) { postdata+= key +"=" + querystring.Get(key)+"&"; } } Uri uri = new Uri(url+postdata); string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); webrequest.CookieContainer = cookies; webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; // Build up the post message header StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append(""); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); sb.Append("\"; filename=\""); sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append(""); sb.Append("Content-Type: "); sb.Append(contenttype); sb.Append(""); sb.Append(""); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); // Build the trailing boundary string as a byte array // ensuring the boundary appears on a line by itself byte[] boundaryBytes = Encoding.ASCII.GetBytes("--" + boundary + ""); FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream(); // Write out our post header requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // Write out the file contents 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); // Write out the trailing boundary requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s); return sr.ReadToEnd(); }
挪用代碼以下:
CookieContainer cookies = new CookieContainer(); //add or use cookies NameValueCollection querystring = new NameValueCollection(); querystring["uname"]="uname"; querystring["passwd"]="snake3"; string uploadfile;// set to file to upload uploadfile = "c:\\test.jpg"; //everything except upload file and url can be left blank if needed string outdata = UploadFileEx(uploadfile, "http://localhost/test.php","uploadfile", "image/pjpeg", querystring,cookies);
至此,一切重要功效代碼都曾經引見終了,至於丑化你的法式就要讀者依據本身的愛好完成了。
作者還供給了吸收文件的PHP代碼,,小我認為既然能寫上傳的法式,吸收文件的Server Page應當不在話下的,何況又不是PHP能力吸收提交的文件,ASP,ASP.NET,JSP都可以,這裡就不再煩瑣了。