Multipart form-data boundary

Source: Internet
Author: User

Description: ENCTYPE = "multipart/form-data" Description:

The rfc1867 protocol overview is uploaded over http, And the content structure sent by the client is constructed.

Overview

In the original http protocol, there was no file upload function. Rfc1867 adds this function to the http protocol. Client browsers, such as Microsoft IE, ILA Ila, and Opera, send user-specified files to the server according to this specification. Web programs on the server, such as php, asp, and jsp, can parse the files sent by the user according to this specification. Microsoft IE, ILA Ila, and Opera already support this protocol. You can use a special form on the webpage to send files. Most http servers, including tomcat, support this protocol and can send files. Various web programs, such as php, asp, and jsp, have encapsulated the uploaded files.

Instance

<form action="upFile.php" enctype="multipart/form-data" method="post"><input name="myflie" type="file" /><input type="submit" />

Note enctype = "multipart/form-data", method = post, type = "file ". According to rfc1867, these three attributes are required. Multipart/form-data is a new encoding type to improve the transmission efficiency of binary files.

Data Transmission

The format must be a word, including the last carriage return.

Note: Content-Length: 226 here 226 is the total Length of the Red Content (including the last carriage return)

Note This line:

Content-Type: multipart/form-data; boundary=-----------------------------264141203718551

According to rfc1867, multipart/form-data is required. --------------------------- 7d33a816d302b6 is a delimiter that separates multiple files and form items.

Among them, 7d33a816d302b6 is a string generated in real time to ensure that the entire separator does not appear in the content of the file or form item.

Use POST to send data

The POST method is mainly used to send a large amount of client data to the server, which is not limited by the URL length. The post request places the data in the HTTP body in URL encoding format. The field format is fieldname = value, and each field is separated. Note that all fields are processed as strings. In fact, what we need to do is simulate the browser to POST a form. The following is a POST request sent by IE to a login form:

POST http://127.0.0.1/login.do HTTP/1.0Accept: image/gif, image/jpeg, image/pjpeg, */*Accept-Language: en-us,zh-cn;q=0.5Content-Type: application/x-www-form-urlencodedUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)Content-Length: 28
 
username=admin&password=1234

To simulate the browser sending this POST request in the MIDP application, first set the HTTP Connection Request Method to POST (PHP is curl ):

// Use the POST method httpURLConnection. setRequestMethod ("POST ");

Then construct the HTTP body:

byte[] data = "username=admin&password=1234".getBytes();

And calculate the body Length, and fill in Content-Type and Content-Length:

httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));

Enable OutputStream to write the body:

OutputStream output = hc.openOutputStream();output.write(data);

Note that the data still needs to be encoded in URL encoding format. Because the MIDP library does not have the URLEncoder class corresponding to J2SE, you need to write the encode () method by yourself, for more information, see java.net. URLEncoder. java source code. The rest is to read the server response, and the code is consistent with GET, so we will not detail it here.

Use multipart/form-data to send files

To upload files to the server on the MIDP 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:

httpURLConnection.setRequestProperty("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:

-- ABCDContent-Disposition: form-data; name = "title" \ r \ nToday -- ABCDContent-Disposition: form-data; name = "1.txt"; filename =" C: \ 1.txt" Content-Type: text/plain \ r \ n <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 the Sniffer program to check the POST request sent by IE, you can find 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 a file due to a separator in the uploaded file. We can write a fixed separator as long as it is complex enough.

The POST code for sending the file is as 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; isb = 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 Server Response: // TODO...

 

 

Reference: http://hi.baidu.com/ycvwvuykhkbadmd/item/e441cc0e36fcd6dd73e676bd

Reprinted please indicate the source: http://www.cnblogs.com/yydcdut/p/3736667.html

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.