WebClient:1. Post String Public stringSENDFIELD_WC (stringUrlstringCookiedata,stringpostdata) {WebClient mywebclient=NewWebClient (); MYWEBCLIENT.HEADERS.ADD ("Cookies", Cookiedata); //Add an articleMYWEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded"); //sent characters to byte stream byte[] SendData = encoding.getencoding ("Utf-8"). GetBytes (Postdata.tostring ()); //lengthMYWEBCLIENT.HEADERS.ADD ("ContentLength", sendData.Length.ToString ()); //Submit byte[] Recdata = Mywebclient.uploaddata (URL,"POST", SendData); //Show return value note encoding return(Encoding.GetEncoding ("Utf-8"). GetString (Recdata)); }2. Post Files Public stringSENDFILE_WC (stringUrlstringCookiedata,stringFilePath) {WebClient mywebclient=NewWebClient (); MYWEBCLIENT.HEADERS.ADD ("Cookies", Cookiedata); byte[] Responsearray = Mywebclient.uploadfile (URL,"POST", FilePath); stringResmsg = encoding.getencoding ("Utf-8"). GetString (Responsearray); returnresmsg; }httpwebrequest:cookie Processing Method:PrivateCookiecontainer Geturicookiecontainer (Uri Uri,stringcookiedata) { //defines a collection of Cookiecollection objects to provide a containerCookiecontainer cookies =NULL; //determine if a string exists if(Cookiedata.length >0) { //instantiating a Cookiecollection objectcookies =NewCookiecontainer (); //break out a cookie string string[] cooks = cookiedata.split (';'); //Traverse all Arrays for(inti =0; I < cooks. Length; i++) { //Replace a comma in a characterCooks[i] = cooks[i]. Replace (",","%2c"); //Set Cookie Informationcookies. Setcookies (URI, cooks[i]); } } returncookies; }1. Post String Public stringSENDFIELD_WBR (stringUrlstringCookiedata,stringpostdata) { byte[] ByteArray =Encoding.UTF8.GetBytes (postdata); Try{HttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); //Set CookiesUri uri =NewUri (URL); Request. Cookiecontainer=Geturicookiecontainer (URI, cookiedata); Request. ContentType="application/x-www-form-urlencoded"; Request. Timeout=30000; Request. Method="POST"; Request. ContentLength=bytearray.length; Stream Requeststream=request. GetRequestStream (); requestStream.Write (ByteArray,0, bytearray.length); Requeststream.close (); HttpWebResponse WebResponse=(HttpWebResponse) request. GetResponse (); StreamReader Reader=NewStreamReader (WebResponse.GetResponseStream (), Encoding.UTF8); String Sresult=Reader. ReadToEnd (); Reader. Close (); returnSresult; } Catch(Exception ex) {return "<error>"+ ex. Message +"</error>"; } } 2. Post file (reference: http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html) Private stringSENDFILE_WBR (stringUrlintTimeOut,stringFilekeyname,stringFilePath, NameValueCollection stringdict,stringcookiedata) { stringresponsecontent; varMemstream =NewMemoryStream (); varWebRequest =(HttpWebRequest) webrequest.create (URL); //Set Cookie information???Uri uri =NewUri (URL); Webrequest.cookiecontainer=Geturicookiecontainer (URI, cookiedata); //Boundary character varBoundary ="---------------"+ DateTime.Now.Ticks.ToString ("x"); //Boundary character varBeginboundary = Encoding.ASCII.GetBytes ("--"+ Boundary +"\ r \ n"); varFileStream =NewFileStream (FilePath, FileMode.Open, FileAccess.Read); //the last Terminator varEndboundary = Encoding.ASCII.GetBytes ("--"+ Boundary +"--\r\n"); //Setting PropertiesWebrequest.method ="POST"; Webrequest.timeout=TimeOut; Webrequest.contenttype="multipart/form-data; boundary="+boundary; //Write File Const stringFilepartheader ="Content-disposition:form-data, name=\ "{0}\"; filename=\ "{1}\" \ r \ n"+"content-type:application/octet-stream\r\n\r\n"; varHeader =string. Format (Filepartheader, Filekeyname, FilePath); varHeaderbytes =Encoding.UTF8.GetBytes (header); Memstream.write (Beginboundary,0, beginboundary.length); Memstream.write (Headerbytes,0, Headerbytes. Length); varBuffer =New byte[1024x768]; intBytesread;//=0 while(Bytesread = filestream.read (buffer,0, buffer. Length))! =0) {memstream.write (buffer,0, Bytesread); } //The key that writes the string varStringkeyheader ="\r\n--"+ Boundary +"\r\ncontent-disposition:form-data; name=\ "{0}\""+"\r\n\r\n{1}\r\n"; foreach(byte[] Formitembytesinch from stringKeyinchStringdict.keysSelect string. Format (Stringkeyheader, Key, Stringdict[key]) into Formitem SelectEncoding.UTF8.GetBytes (Formitem)) {Memstream.write (formitembytes,0, Formitembytes. Length); } //writes the last end of the bounding characterMemstream.write (Endboundary,0, endboundary.length); Webrequest.contentlength=memstream.length; varRequeststream =Webrequest.getrequeststream (); Memstream.position=0; varTempbuffer =New byte[Memstream.length]; Memstream.read (Tempbuffer,0, tempbuffer.length); Memstream.close (); requestStream.Write (Tempbuffer,0, tempbuffer.length); Requeststream.close (); varHttpWebResponse =(HttpWebResponse) webrequest.getresponse (); using(varHttpstreamreader =NewStreamReader (Httpwebresponse.getresponsestream (), Encoding . GetEncoding ("Utf-8")) {responsecontent=Httpstreamreader.readtoend (); } filestream.close (); Httpwebresponse.close (); Webrequest.abort (); returnresponsecontent; }
Post methods for HttpWebRequest and WebClient in C #