HTTP POST request message format Analysis and Java implementation file upload

Source: Internet
Author: User
Tags diff http post

time 2014-12-11 12:41:43 csdn Blog Original http://blog.csdn.net/bboyfeiyu/article/details/41863951 ThemeHTTPhttpcomponents

In development, the more HTTP requests we use are basically get, POST. Where get is used to fetch data from the server, post is used primarily to submit some form data to the server, such as file uploads. And we encountered in the use of HTTP requests in the more troublesome thing is to construct the file upload HTTP message format, although the format is relatively simple, but also more error-prone. Today we will learn the message format for HTTP post and the request to simulate file uploads via Java.

First we look at a POST request for a message, and then we'll analyze it in detail.

Post message Format
POST/api/feed/http/1.1Accept-encoding:GzipContent-length:225873Content-type:Multipart/form-data; Boundary=ocqxmf6-jxtxomdhmog5w5ey9mgrstbpHost:Www.myhost.comConnection:Keep-alive--ocqxmf6-jxtxomdhmog5w5ey9mgrstbpcontent-disposition:form-data; name= "LNG" Content-Type: Text/plain; Charset=utf-8content-transfer-encoding: 8bit116.361545-- Ocqxmf6-jxtxomdhmog5w5ey9mgrstbpcontent-disposition:form-data; Name= "lat" content-type:text/plain; Charset=utf-8content-transfer-encoding: 8bit39.979006-- Ocqxmf6-jxtxomdhmog5w5ey9mgrstbpcontent-disposition:form-data; Name= "Images"; Filename= "/storage/emulated/0/camera/jdimage/1xh0e3yyfmpr2e35tdowbavrx.jpg" content-type:application/ Octet-streamcontent-transfer-encoding:binary here is the binary data of the picture -- ocqxmf6-jxtxomdhmog5w5ey9mgrstbp--           

Here we submit a longitude, latitude, and a picture (the image data is long and messy, omitted here).

Format Analysis Request Header analysis

Let's read the first line in the text format:

/api/feed/http/1.1
This line describes how this request is requested, that is, the Post method, the requested subpath is/api/feed/, for example, our server address is www.myhost.com, and then our full path to this request is www.myhost.com/api/feed /, finally illustrates the HTTP protocol version number is 1.1.
Gzip225873multipart/form-data; BOUNDARY=OCQXMF6-JXTXOMDHMOG5W5EY9MGRSTBPwww.myhost.com  Keep-alive   
These headers mean that the data returned by the server needs to use gzip compression, the content length of the request is 225873, the content type is "Multipart/form-data", the request parameter delimiter (boundary) For OCQXMF6-JXTXOMDHMOG5W5EY9MGRSTBP, the root domain of the request is www.myhost.com, and the HTTP connection is persistent (keep-alive).

One of the things to note here is the delimiter, which is boundary. Boundary is used as the boundary identifier between the request parameters, such as the need for a definite boundary between parameter 1 and parameter 2, so that the server resolves to parameter 1 and parameter 2 correctly. But the delimiter is not just boundary, but the following format:--+ boundary. For example, here the boundary is OCQXMF6-JXTXOMDHMOG5W5EY9MGRSTBP, then the parameter delimiter is:

--ocqxmf6-jxtxomdhmog5w5ey9mgrstbp
Whether or not the boundary itself has this "--", this "--" can not be omitted.

We know that the HTTP protocol uses "request-answer" mode, when using normal mode, that is, non-keepalive mode, each request/reply client and server to create a new connection, immediately after the completion of the connection (HTTP protocol is a non-connected protocol) When using Keep-alive mode (also known as persistent connection, connection reuse), the Keep-alive feature keeps the client-to-server connection active, and the keep-alive feature avoids establishing or re-establishing a connection when subsequent requests to the server occur.

As in, the left is the case of the shutdown keep-alive, each request will need to establish a connection, and then close the connection, the right is keep-alive, after the first establishment of the request to remain connected, and then the subsequent do not need to establish, close the connection each time, Enabling the Keep-alive mode is certainly more efficient and performance-based because it avoids the overhead of establishing/releasing connections.

HTTP 1.0 is turned off by default, you need to add "connection:keep-alive" in the HTTP header to enable Keep-alive;http 1.1 by default enabling Keep-alive, if you join "Connection:close", Before closing. Most browsers now use the http1.1 protocol, which means that the Keep-alive connection request is initiated by default, so whether a full keep-alive connection can be completed depends on the server setup.

Request Entity Analysis

The request entity is actually the list of parameters for the HTTP POST request, with each parameter starting with the request delimiter, i.e.-+ boundary. For example, the following parameter.

--ocqxmf6-jxtxomdhmog5w5ey9mgrstbpcontent-disposition:form-data; Name= "LNG" content-type:text/plain; Charset=utf-8bit116.361545   
Above the first behavior--OCQXMF6-JXTXOMDHMOG5W5EY9MGRSTBP, that is--plus boundary content, and finally add a newline (this line break can not be omitted), the string of newline is expressed as "\ r \ n". The second behavior is content-disposition and the parameter name, here the parameter is called LNG, that is longitude. Content-disposition is to provide a default file name when the user wants to save the requested content as a file, and we don't pay much attention here. The third behavior is Content-type, which is the type of object that the WEB server tells the browser to respond to, and the specified character encoding is UTF-8. The four lines describe the transmission of the entity objects (entities) that accompany the message request and the response (response), the simple text data we set to 8bit, the file parameters we set to binary on the line. Then add two line breaks after the specific contents of the parameter. For example, the parameter content here is 116.361545.

Note that each row here is wrapped with "\ r \ n", with two line breaks between the last line and the parameter content. File parameters are the same format, except that the contents of the file parameters are byte streams.

Note here that the normal text parameters and file parameters are different in the following two places, because the format of the content itself is not the same.

General parameters:

Text/plain; Charset=utf-88bit

File parameters:

Application/octet-streambinary

The last line of the parameter entity is:-Plus boundary plus--, the last line, here is the format:--OCQXMF6-JXTXOMDHMOG5W5EY9MGRSTBP--。

Analog File Upload Request
PublicStaticvoid UploadFile (String fileName) {try {  Line breakFinal String NewLine ="\ r \ n";Final String Boundaryprefix ="--"; Defining a data separator lineString boundary ="========7D4A6D158C9"; The domain name of the serverURL url =New URL ("Www.myhost.com");HttpURLConnection conn = (httpurlconnection) url.openconnection (); Set to postConn.setrequestmethod ("POST"); To send a POST request, you must set the following two linesConn.setdooutput (true);Conn.setdoinput (true);Conn.setusecaches (FALSE); Set Request Header ParametersConn.setrequestproperty ("Connection","Keep-alive");Conn.setrequestproperty ("Charsert","UTF-8");Conn.setrequestproperty ("Content-type","Multipart/form-data; boundary= "+ boundary);OutputStreamout =New DataOutputStream (Conn.getoutputstream ()); Uploading filesFile File =New File (FileName);StringBuilder SB =New StringBuilder ();Sb.append (Boundaryprefix);Sb.append (boundary);Sb.append (NewLine); File parameters, photo parameter name can be arbitrarily modifiedSb.append ("Content-disposition:form-data;name=\" photo\ "filename=\" + filename+"\" "+ newLine);Sb.append ("Content-type:application/octet-stream"); After the parameter header is set, you need two newline, then the parameter contentSb.append (NewLine);Sb.append (NewLine); Writes the data of the parameter header to the output stream Out.write (Sb.tostring (). GetBytes ()); Data input stream for reading file dataDataInputStreamin =New DataInputStream (New FileInputStream (file)); byte[] Bufferout =Newbyte[1024]; int bytes =0; Reads 1KB of data at a time and writes file data to the output stream while (bytes =In.read (bufferout))! =-1) { Out.write (Bufferout,0, bytes);} Last Add line break Out.write (Newline.getbytes ()); In.close (); Define the last data divider, i.e.-plus boundary plus--。 Byte[] End_data = (newLine + boundaryprefix + boundary + Boundaryprefix + newLine). GetBytes (); //write end identification  out.write (end_ data);  out.flush ();  out.close ();  //define BufferedReader input stream to read the response of the URL // BufferedReader reader = new BufferedReader (new InputStreamReader (//Conn.getinputstream ())); span class= "comment" >//string line = null; //while ((line = Reader.readline ())! = null) {//System.out.println (line) ; //}} catch (Exception e) { System. Out.println ( "send POST request exception! "+ e);  E.printstacktrace (); } 
Uploading files using Apache Httpmime
/**     *@param fileName Image Path */PublicStaticvoid Uploadfilewithhttpmime (String fileName) {Define request URL String uri ="Www.myhost.com";Instantiating an HTTP client HttpClient HttpClient =New Defaulthttpclient ();Instantiate post submission mode HttpPost post =New HttpPost (URI);Adding JSON parameterstry { Instantiating a Parameter objectmultipartentity params =New Multipartentity (); Picture text parametersParams.addpart ("Textparams",New Stringbody ( "{' user_name ': ' My username ', ' channel_name ': ' But TD ', ' channel_address ': ' (123.4,30.6) '} ',Charset.forname ("UTF-8"))); Set Upload fileFile File =New File (FileName); File parameter ContentsFilebody Filebody =New Filebody (file); Add File ParametersParams.addpart ("Photo", filebody);Params.addpart ("Photoname",New Stringbody (File.getname ())); Adding parameters to the POST request bodyPost.setentity (params); Perform a POST request and get the return object [we're going to start our request at this step]HttpResponse resp = Httpclient.execute (POST); Parse return request resulthttpentity entity = resp.getentity ();InputStream is = Entity.getcontent ();BufferedReader reader =new bufferedreader (new inputstreamreader (IS));  StringBuffer buffer = new stringbuffer ();  String temp;  while ((temp = Reader.readline ())! = null) { Buffer.append (temp); } System.out.println (buffer);} catch (unsupportedencodingexception e) { E.printstacktrace ();} catch (clientprotocolexception e) { E.printstacktrace ();} catch (IOException e) { E.printstacktrace ();} catch (illegalstateexception e) { E.printstacktrace ();}}    

Httpmime.jar,

Download the HttpClient package, which is included in the Httpmime.jar.

Transfer from: Link

HTTP POST request message format Analysis and Java implementation file upload

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.