Extract parameters and files from the Post Data Stream

Source: Internet
Author: User

The data submitted by form, whether it is application/X-WWW-form-urlencoded or multipart/form-data (when there is an attachment), can be submitted through the request on the server. form ["name"] and request. files ["name"] gets parameters and uploaded files. The same applies to post requests. However, sometimes the format of post data may not be very standard (maybe, not too detailed), and data cannot be obtained on the server. Recently, I encountered such a problem that a data partner simulated post.

In desperation, I think of a next strategy, that is, to extract the desired data by analyzing the received input stream. For example, the data I mentioned above (two parameters, one image file)

Implementation idea: Read the input stream, analyze the non-file part, determine the location and size of the file in the whole stream, and then re-read the input stream to get the file.

Look at the post data, parameters, and delimiters above. To put it bluntly, they are all strings. It is basically an English numeric symbol. If there is a Chinese character, you can encode it before sending it. This basically ensures that the calculation error is not caused by the encoding problem (all English characters are encoded in the same way ). This part of content (Non-file partThe file is in binary format. Do not do this.

By accepting input streams into strings, if the parameters contain Chinese characters, pay attention to the encoding used by the client post:

  Byte[] Input=Request. binaryread (request. totalbytes );
StringSource=Encoding. utf8.getstring (input );

Then, the regular expression is used to match the non-file content above. Here, the obtained content is omitted and then converted to byte [] to calculate its length.ParametersThe value can be obtained here.

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> RegEx rginput = New RegEx ( " omitted " );
If (rginput. ismatch (source)
{< br> int headlength = encoding. utf8.getbytes (rginput. match (source ). value)
}

Similarly, calculate the length of "--------------------------- 7da119c1004a6 --" (each request should be fixed ).Note that there are line breaks.

In this way, the size of other parts of the file in the whole stream can be determined, that is, the location and size of the file in the whole stream are also determined. Then, you can obtain it by re-reading the original stream.FILE Section. If the file is of the text type, you can analyze and obtain it directly like the parameter section.

  //Save files
Filestream FSS= NewFilestream ("Path", Filemode. Create );
FSS. Write (input, headlength, input. Length-Headlength-Footlength );
FSS. Close ();

 

Code

  //  Get the object byte array  
Byte [] Imgcont = New Byte [Input. Length - Headlength - Footlength];
Memorystream MS = New Memorystream (input );
// Move the cursor to the beginning of the file
Ms. Seek (headlength, seekorigin. Begin );
Ms. Read (imgcont, 0 , Imgcont. Length );
Ms. Close ();

Note: There is a certain risk in this method, that is, the determination of the file size and location must be accurate, and the analysis of the input stream should be thoughtful and comprehensive; and when multiple files are uploaded, this method is troublesome... if you have a better method, you can leave a message.

  Keywords: Simulate post, simulate file upload, multipart/form-data, request. Form, request. Files, read parameters and files from the stream
Http://chy710.cnblogs.com

 

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.