C # simulate submission form post-form with attachment

Source: Internet
Author: User
Tags form post

To simulate the submission of a form, you only need to construct the data in the HTML Protocol. However, if the form contains file, that is to say, how to submit the content that contains the uploaded attachment?

Use multipart/form-data to send files

To upload files to the server on the client, we must simulate a post multipart/form-data request. The content-type must be multipart/form-data.

The format of post requests encoded with multipart/form-data is completely different from that of application/X-WWW-form-urlencoded. To use multipart/form-data, you must first set a separator in the HTTP request header, for example, ABCD:

We need to set the Content-Type for the simulated submission to be different from the Content-Type for post with non-attachments. here we need to: ("Content-Type", "multipart/form-data; boundary = ABCD ");

Then, each field is separated by "-- separator", and the last "-- Separator --" indicates the end. For example, to upload a title field "today" and a file c: \ 1.txt, the HTTP body is as follows:

-- ABCD

Content-Disposition: Form-data; name = "title"

\ R \ n

Today

-- ABCD

Content-Disposition: Form-data; name = "1.txt"; filename =" C: \ 1.txt"

Content-Type: text/plain

\ R \ n

<This is the content of the 1.txt File>

-- ABCD --

\ R \ n

Note that each row must end with \ r \ n, including the last row.

If you use snifferProgramCheck the POST request sent by IE. It can be found that the IE separator is similar to -- 7d4a6d158c9, which is a random number generated by IE. The purpose is to prevent the server from correctly identifying the start position of the file due to the Separator in the uploaded file. We can write a fixed separator as long as it is complex enough.

Below is a Java Post with an attachment to simulate the sending program segment:

Post of the sent fileCodeAs follows:

String [] props =... // field name
String [] values =... // Field Value
Byte [] file =... // File Content
String boundary = "--------------------------- 7d4a6d158c9"; // delimiter
Stringbuffer sb = new stringbuffer (); // send each field
: For (INT I = 0; I sb = sb. append ("--");
SB = sb. append (Boundary );
SB = sb. append ("\ r \ n ");
SB = sb. append ("content-Disposition: Form-data; name = \" "+ props [I] +" \ "\ r \ n ");
SB = sb. append (urlencoder. encode (Values [I]);
SB = sb. append ("\ r \ n ");
} // Send the file: SB = sb. append ("--");
SB = sb. append (Boundary );
SB = sb. append ("\ r \ n ");
SB = sb. append ("content-Disposition: Form-data; name = \" 1 \ "; filename = \" 1.txt \ "\ r \ n ");
SB = sb. append ("Content-Type: Application/octet-stream \ r \ n ");
Byte [] DATA = sb. tostring (). getbytes ();
Byte [] end_data = ("\ r \ n --" + boundary + "-- \ r \ n"). getbytes (); // set the HTTP header:
HC. setrequestproperty ("Content-Type", multipart_form_data + ";
Boundary = "+ boundary );
HC. setrequestproperty ("Content-Length", String. valueof (data. Length + file. Length + end_data.length ));
// Output: Output = HC. openoutputstream (); output. Write (data); output. Write (File); output. Write (end_data );
// Read the server response:
// Todo...

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.