C#采用HttpWebRequest实现保持会话上传文件到HTTP的方法
程序员文章站
2024-02-17 08:56:10
本文实例讲述了c#采用httpwebrequest实现保持会话上传文件到http的方法,在项目开发中有一定的实用价值,具体方法如下:
一、前言:
这篇文章翻译来自mad...
本文实例讲述了c#采用httpwebrequest实现保持会话上传文件到http的方法,在项目开发中有一定的实用价值,具体方法如下:
一、前言:
这篇文章翻译来自madmik3 写在 codeproject 上的文章,原标题为: c#'s webclient.uploadfile with more functionality.
二、正文:
我们使用 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都可以,这里就不再啰嗦了。