1.httpclient is a toolkit that can send HTTP connections, including the ability to send post requests and get requests.
An HTTP connection once has a return stream. HTTP is a two-way street. Only if the connection is made, there will be an output return stream.
So when an HTTP connection is executed, the return value is the return stream of the HTTP connection.
HttpResponse response = Client.execute (HttpPost);
2.http send, the body can be written in Chinese. But note the garbled problem:
- Public static String gethttprequeststring (String url,string body) throws IOException {
- HttpClient client = new Defaulthttpclient ();
- HttpPost HttpPost = new HttpPost (URL);
- Stringentity stringentity = new stringentity (body);
- Httppost.setentity (stringentity);
- Httppost.setheader ("Content-type", "Application/json; Charset=utf-8 ");
- HttpResponse response = Client.execute (HttpPost);
- BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (Response.getentity (). GetContent ()) );
- String Line;
- StringBuffer jsonstring = new StringBuffer ();
- While (line = Bufferedreader.readline ()) = null) {
- Jsonstring.append (line);
- }
- return jsonstring.tostring ();
- }
This is the original code, if the body of the transmission of Chinese characters, if the other side set the format is UTF-8, then he received the characters are garbled,
Stringentity.setcontentencoding ("UTF-8");
Add such a code, set the next format is good.
HttpClient is a toolkit that can send HTTP connections, including the ability to send post requests and get requests