C # code, simulate form form send POST request, upload file (with other parameters)

Source: Internet
Author: User

I do not understand the post, some time ago encountered a need to send a POST request in C # code to upload a file business, so reference several posts, coupled with their own practice to write the following code. Write the comparison low hope you greatly corrected ^_^.

Business requirements:

The other side gave an interface, let pass four parameters for "ModelID, Filecontent, UpdateTime, encrypt"

Where ModelID, UpdateTime, and encrypt are common string types . Filecontent is a binary file

I really do not understand the post of the relevant knowledge, usually just ordinary usage, no deep read the relevant knowledge.

So I use the HTML page to write a form form submission, and then intercept to see the request header and the content of the request body (this is just a normal text file so fileconten can display ordinary text, If it's a picture or something like this, it's garbled.

This is where I think it's more important.

Looking at this information, I can tell

In the request header:

Content-type:multipart/form-data

Boundary=ceshi( sense should be the meaning of the delimiter, "Ceshi" is I casually write )

Because ultimately the send request Flow , the contents of these request bodies need to be written into the request stream, although some things string this text type of data, but the file itself is binary, so I put all the text into binary, Plus the file itself ,

It then writes the request stream in the order of this format , and finally sends it out ( if it does not become binary, only text-type file uploads can be made, with a lot of restrictions, and easy to corrupt the file ).

The code is as follows:

Send the 2.html file under the C drive (images, PDFs, etc. are also available, as all are turned into binary) have been tested

Private voidPost1 () {stringURL =@"http://*******";//we're not going to expose our address here.            stringModelID ="4f1e2e3d-6231-4b13-96a4-835e5af10394"; stringUpdateTime ="2016-11-03 14:17:25"; stringEncrypt ="f933797503d6e2c36762428a280e0559"; stringFilePath =@"c:/2.html"; stringFileName ="2.html"; byte[] Filecontentbyte =New byte[1024x768];//file content binary            #regionTurn files into binaryFileStream FS=NewFileStream (FilePath, FileMode.Open, FileAccess.Read); Filecontentbyte=New byte[FS. Length];//Binary FilesFs. Read (Filecontentbyte,0, Convert.ToInt32 (fs.            Length)); Fs.            Close (); #endregion            #regionDefine the content in the request body and turn it into binarystringBoundary ="Ceshi"; stringEnter ="\ r \ n"; stringModelidstr ="--"+ Boundary +Enter+"content-disposition:form-data; name=\ "modelid\""+ Enter +Enter+ ModelID +Enter; stringFilecontentstr ="--"+ Boundary +Enter+"Content-type:application/octet-stream"+Enter+"Content-disposition:form-data, name=\ "filecontent\"; filename=\ ""+ FileName +"\""+ Enter +Enter; stringUpdatetimestr = Enter +"--"+ Boundary +Enter+"content-disposition:form-data; name=\ "Updatetime\""+ Enter +Enter+UpdateTime; stringEncryptstr = Enter +"--"+ Boundary +Enter+"content-disposition:form-data; name=\ "Encrypt\""+ Enter +Enter+ Encrypt + Enter +"--"+ Boundary +"--"; varModelidstrbyte = Encoding.UTF8.GetBytes (MODELIDSTR);//ModelID all strings in binary            varFilecontentstrbyte = Encoding.UTF8.GetBytes (FILECONTENTSTR);//filecontent binary of information such as some names (not including the file itself)            varUpdatetimestrbyte = Encoding.UTF8.GetBytes (UPDATETIMESTR);//UpdateTime all strings in binary                        varEncryptstrbyte = Encoding.UTF8.GetBytes (ENCRYPTSTR);//encrypt all strings in binary            #endregionHttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); Request. Method="POST"; Request. ContentType="multipart/form-data;boundary="+boundary; Stream Myrequeststream= Request. GetRequestStream ();//defining the request Flow            #regionConvert each binary sequence write request stream modelidstr, Uodatetimestr, encryptstr (Filecontentstr + filecontent)Myrequeststream.write (Modelidstrbyte,0, modelidstrbyte.length); Myrequeststream.write (Filecontentstrbyte,0, filecontentstrbyte.length); Myrequeststream.write (Filecontentbyte,0, filecontentbyte.length); Myrequeststream.write (Updatetimestrbyte,0, updatetimestrbyte.length); Myrequeststream.write (Encryptstrbyte,0, encryptstrbyte.length); #endregionHttpWebResponse Response= (HttpWebResponse) request. GetResponse ();//SendStream Myresponsestream= Response. GetResponseStream ();//Get return valueStreamReader Mystreamreader =NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); stringRetString =Mystreamreader.readtoend ();            Mystreamreader.close ();        Myresponsestream.close (); }

The code has been tested and has been used. This is a modified version, removed some other things, is only for reference role.

If there is any shortage, please correct me greatly.

C # code, simulate form form send POST request, upload file (with other parameters)

Related Article

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.