HttpClient is a toolkit that can send HTTP connections, including the ability to send post requests and get requests

Source: Internet
Author: User

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:

  1. Public static String gethttprequeststring (String url,string body) throws IOException {
  2. HttpClient client = new Defaulthttpclient ();
  3. HttpPost HttpPost = new HttpPost (URL);
  4. Stringentity stringentity = new stringentity (body);
  5. Httppost.setentity (stringentity);
  6. Httppost.setheader ("Content-type", "Application/json;  Charset=utf-8 ");
  7. HttpResponse response = Client.execute (HttpPost);
  8. BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (Response.getentity (). GetContent ())  );
  9. String Line;
  10. StringBuffer jsonstring = new StringBuffer ();
  11. While (line = Bufferedreader.readline ()) = null) {
  12. Jsonstring.append (line);
  13. }
  14. return jsonstring.tostring ();
  15. }

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

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.