HTTP Post Form Experience record

Source: Internet
Author: User
Tags http post

The format of the post form must not change, especially the format of the data section in the form. Last night's bug is because in the form of the data part of the increase in content-length, the superfluous, resulting in the server received the data in the body of the number of characters (more out of the character is Content-length). Content-length should be assigned in the HTTP header. Hereby record.


In addition, the post form is not compressed or the server is not recognized.


Finally, enclose the form in the correct format:

HTTP HEADER:

Content-type:multipart/form-data; boundary=------uploadboundary------



HTTP Body:

--------uploadboundary------

Content-disposition:form-data; Name= "Shopid"


50000

--------uploadboundary------

Content-disposition:form-data; Name= "Name"


Photo Name (Utf-8)

--------uploadboundary------

Content-disposition:form-data; Name= "token"


309afac7f5734be7fea7c660b54f2f42dfa9ae83ed6f9c6e789dad6d4449d397b6b7c434497b2801ac5e5ba0a25aeb6d242910b4d98b40f497f252efe 239603c986fda9083c1eab7b8802dd4ee0c309baaac8b7dbdcc8f24769e661858184267

--------uploadboundary------

Content-disposition:form-data; Name= "Photo"; Filename= "Whatever.jpg"

Content-type:image/jpg

Content-transfer-encoding:binary


JPG data

--------Uploadboundary--------



SAMPLE CODE (JAVA):


String CRLF = "/r/n";

String boundary = "--------uploadboundary------";


Bytearraybuffer buffer = new Bytearraybuffer (data.length + 1000);

Post.setheader ("Content-type", "multipart/form-data; boundary=------uploadboundary------");


Shopid

Buffer.append (boundary);

Buffer.append (CRLF);

Buffer.append ("Content-disposition:form-data; name=/"Shopid/");

Buffer.append (CRLF);

Buffer.append (SHOPID);


Name

Buffer.append (boundary);

Buffer.append (CRLF);

Buffer.append ("Content-disposition:form-data; name=/"name/");

Buffer.append (CRLF);

Buffer.append (Name.tochararray ("Utf-8"));


Token

Buffer.append (boundary);

Buffer.append (CRLF);

Buffer.append ("Content-disposition:form-data; name=/"token/");

Buffer.append (CRLF);

Buffer.append (token);


Data

Buffer.append (boundary);

Buffer.append (CRLF);

Buffer.append ("Content-disposition:form-data; name=/"photo/"; filename=/"whatever.jpg/");

Buffer.append (CRLF);

Buffer.append ("content-type:image/jpg");

Buffer.append (CRLF);

Buffer.append ("Content-transfer-encoding:binary");

Buffer.append (CRLF);

Buffer.append (CRLF);

Buffer.append (data);

Buffer.append (CRLF);

Buffer.append (boundary);

Buffer.append ("--");

Buffer.append (CRLF);


bytearrayentity entity = new Bytearrayentity (Buffer.tobytearray ());

Post.setentity (entity);

return entity;

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.