Multipart/form-data Request analysis of HTTP protocol

Source: Internet
Author: User
Tags http request

http://blog.csdn.net/five3/article/details/7181521

First to understand what a multipart/form-data request is:

According to the http/1.1 RFC 2616 protocol, we only request options,, POST, PUT, DELETE, Trace, and so on, why do we still have the multipart/form-data request to say it. This is going to start from the beginning.


HTTP protocol We all know that is the provision of ASCII code transmission, based on TCP, IP protocol, the application layer specification, the specification content of the HTTP request is divided into 3 departments: status line, request header, request body. All methods and implementations are carried out around how to use and organize the three parts. In other words, it is the change, as long as we understand the components of the HTTP request, nature can strain any actual work needs and problems.

About the status line, request header, request body and other three parts of the specific content, you can refer to the official agreement document HTTP://WWW.FAQS.ORG/RFCS/RFC2616.HTML, here the main analysis of Multipart/form-data request is exactly what the matter.


Since the original method of the HTTP protocol itself does not support the Multipart/form-data request, the request is naturally evolved from these primitive methods, specifically how it evolves and looks at the following:

1, the basic method of Multipart/form-data is post, that is, by the Post method to combine the implementation of

2. The difference between Multipart/form-data and Post methods: request header, request body.

3, the Multipart/form-data request header must contain a special header information: Content-type, and its value must also be specified as Multipart/form-data, It is also necessary to specify that a content separator is used to split the content of multiple posts in the request body, such as the contents of the file and the text content naturally need to be separated, otherwise the receiver will not be able to parse and restore the file. The specific header information is as follows: [HTML] view plain copy content-type:multipart/form-data; Boundary=${bound}

Where ${bound} is a placeholder, on behalf of our specified separator, can be arbitrarily specified, but in order to avoid and normal text duplication, try to use a bit more complex content. Example:--------------------56423498738365


4, Multipart/form-data's request body is also a string, but unlike the POST request body is how it is constructed, post is a simple Name=value value connection, and multipart/ Form-data is a constructor that adds content such as delimiters. The exact format is as follows: [HTML] view plain copy--${bound} content-disposition:form-data; Name= "Filename" Http.pdf--${bound} content-disposition:form-data; Name= "file000"; Filename= "HTTP protocol detailed. pdf" Content-type:application/octet-stream%pdf-1.5 file Content%%eof--${bound} Co Ntent-disposition:form-data; Name= "Upload" Submit Query--${bound}--where ${bound} is the delimiter in the previous header information, if the header information is specified as 123, then this is also 123,; it is easy to see that this request body is composed of several identical parts: each A part begins with a delimiter, then a description of the content of that part, followed by a carriage return, followed by a description of what the information is, and if the content is a file, it also contains the file name information, as well as the type of the document content. The second small part of the above is actually the structure of a file body, and finally, the end of the-----------delimiter.


In summary, you can know to send a multipart/form-data request, in fact, any support POST request tool or language can support, just want to pack a little bit.


Attention:

1, in C language read Multipart/form-data request input, can not use getline function, to use GetChar to determine whether the file is the end of EOF.

2, Multipart/form-data request data can only be read once, so to save up.

3, the input has \ r \ n or other characters, some commands cannot be viewed (such as logger and tail), can be saved as a file, and then view.


References:

Baidu Encyclopedia: http://baike.baidu.com/view/9472.htm

HTTP1.1 protocol Specification: http://www.faqs.org/rfcs/rfc2616.html analysis tool: Httpanalyzer

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.