Depending on the origin of the entity, HttpClient divides the entity into three categories:
Streamed-the entity content is generated from the stream receiving or re-running. In particular, such entities include those received from the HTTP response. Streamed entities are usually non-repeatable to read.
Self-contained-the entity content is present in memory or obtained through a separate connection or other entity. Self-contained entities are repeatable to read. Such entities are often used for HTTP requests in entity encapsulation.
Wrapping-Entity content is obtained from other entities.
This difference is important for connection management when you get streaming content from an HTTP response. The difference between streamed entities and self-contained entities is less important for request entities created by the application and used only by HttpClient. In this case, it is recommended that non-repeatable reads are considered to be streamed entities, which can be read repeatedly as self-contained entities.
The Common httpentitystringentity
New Stringentity ("Important Message", Contenttype.create ("Text/plain", "UTF-8")); New HttpPost ("http://www.example.com/"); httppost.setentity (entity);
Urlencodedformentity
New Arraylist<namevaluepair>(); Formparams.add (new basicnamevaluepair ("param1", "value1") ); Formparams.add (new basicnamevaluepair ("param2", "value2"new Urlencodedformentity (Formparams, consts.utf_8);
Fileentity
File File = new file ("Hello.txt"); httpentity entity = new fileentity (file, Contenttype.create ("Text/plain", "UTF-8"));
Bytearrayentity
New Bytearrayentity ("Important Message". GetBytes ());
Bytearrayinputstream
New Bytearrayinputstream ("Important Message". GetBytes ());
Serializableentity
New Serializableentity ("important message");
Entitybuilder
You can create httpentity instances from Entitybuilder, Entitybuilder has several setter methods for setting entity content, which are mutually exclusive, and if these methods are called more than once, only the last call will work.
SetText (String)
Setbinary (byte[])
SetStream (Java.io.InputStream)
Setserializable (java.io.Serializable)
Setparameters (java.util.list<namevaluepair>)
Setparameters (Org.apache.http.NameValuePair ...)
Setfile (Java.io.File)
Using Entitybuilder
httpentity entity = entitybuilder.create () . SetText ("Important Message") . setContentType ( Contenttype.create ("Text/plain", "UTF-8")) . Build ();
HttpClient (4.3.5)-HTTP Entity