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...