C # uploading files with HttpWebRequest custom header information

Source: Internet
Author: User

Just when the internship encountered a problem: Upload the file URL has a specific format restrictions, you need to format the header information, to upload the success. Check the data when it was like a Java version, C # code affixed.

//uploadfile: Upload file name Groupid:guid Public stringFileUpload (stringUploadFile,stringgroupId) {    //reads a block of bytes from the stream and writes the data to the given bufferFileStream FileStream =NewFileStream (UploadFile, FileMode.Open, FileAccess.Read); byte[] Buffer=NewByte[filestream.length]; FileStream.Read (Buffer,0, Convert.ToInt32 (filestream.length)); //Boundary Setting    stringBoundary ="----------"+ DateTime.Now.Ticks.ToString ("x"); stringFileName =Path.getfilename (UploadFile); stringNameonly = filename.substring (0, Filename.lastindexof (".")); Dictionary<string,string> Params =Newdictionary<string,string>(); Params.add ("name", FileName); Params.add ("groupId", groupId); Params.add ("Appmodule","Portal"); Params.add ("FilePath",""); Params.add ("thumbnail","true"); Params.add ("Storetype","1"); Params.add ("property1",""); Params.add ("Property2",""); Params.add ("Property3",""); Params.add ("Property4",""); Params.add ("property5",""); Params.add ("Flag","false"); StringBuilder SB=NewStringBuilder (); //Send required Fields    foreach(keyvaluepair<string,string> paraminchParams) {SB= sb. Append ("--"); SB=sb.        Append (boundary); SB= sb. Append ("\ r \ n"); SB= sb. Append ("content-disposition:form-data; name=\ ""+ param. Key +"\ "\r\n\r\n"); SB=sb. Append (param.        Value); SB= sb. Append ("\ r \ n"); }    //the file to sendSB = sb. Append ("--"); SB=sb.    Append (boundary); SB= sb. Append ("\ r \ n"); SB= sb. Append ("content-disposition:form-data; name=\ ""+ Nameonly +"\ "; filename=\""+ FileName +"\ "\ r \ n"); SB= sb. Append ("content-type:application/octet-stream\r\n\r\n"); byte[] data =Encoding.Default.GetBytes (sb.)    ToString ()); byte[] End_data = Encoding.Default.GetBytes (("\r\n--"+ Boundary +"--\r\n")); Uri URI=NewUri ("Upload file Address"); HttpWebRequest Webrequset=(HttpWebRequest) webrequest.create (URI); Webrequset.contenttype="multipart/form-data; boundary="+boundary; Webrequset.method="POST"; //Length Properties: Header + file stream + tail length    LongLength = data. Length + buffer. Length +End_data.    Length; Webrequset.contentlength=length; Stream Requeststream=Webrequset.getrequeststream (); //writes the stitched data and the file streamrequestStream.Write (data,0, data.    Length); requestStream.Write (Buffer,0, buffer.    Length); requestStream.Write (End_data,0, End_data.    Length); //Get return informationWebResponse responce =Webrequset.getresponse (); Stream Responestream=responce.    GetResponseStream (); StreamReader SR=NewStreamReader (Responestream); returnSr. ReadToEnd ();}

C # uploading files with HttpWebRequest custom header information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.