About HttpClient's learning experience, request parameter Chinese garbled problem

Source: Internet
Author: User
Tags connection pooling

A third-party interface has recently been called, and the interface document qualifies the HTTPS POST request parameter to be in JSON format. And then casually found a copy of the HttpClient tool code, and then directly call the POST request, the parameters also feel no problem, the return value is normal, but later found that the parameters of the Chinese all became a question mark passed, and later found that the parameter is passed when the encoding is not specified, Now recorded for later learning to see, have encountered and I have the same problem of beginners can reduce the time to ask questions.

The code is as follows:

  HttpClient HttpClient = null;
HttpPost httppost = null;
String result = null;
HttpResponse response = null;
try {
     
Httpclient=new defaulthttpclient ();//1 Create HttpClient Object
HttpPost = new HttpPost (URL);//2 create HttpPost object because it is a POST request
  httppost.addheader ("Content-type", "Application/json"),//3 set request header parameter and parameter type 
stringentity se = new S Tringentity (Jsonstr, "UTF-8");//4 set the contents of the parameters, and develop the encoding format (before the problem is missing the "UTF-8" This parameter, there is a question mark)
Se.setcontenttype ("Text/json" );//format Type
se.setcontentencoding (new Basicheader ("Content-type", "Application/json"));
Httppost.setentity (SE);//parameter encapsulated into post
response = Httpclient.execute (HttpPost);//Execute Request method, return response response parameter
if (response.getentity ()!=null) {//Determines whether the response is null
result = Entityutils.tostring (Response.getentity (), CharSet);//Refers to The response parameter encoding is then returned
}
} catch (IOException e) {
E.printstacktrace ();
}
return result;
}


follow-up and third-party technicians asked for a demo of their company, studied and learned a bit, found that they were using the Poolinghttpclientconnectionmanager connection pool
checked the data to know that frequent creation of the connection (three handshake) disconnection (four waves) will consume a lot of resources, so the use of connection pooling technology to facilitate the next call.

public static void Init () {//demo, which sets the relevant request parameters and number of connections in the initialization method

Connmgr = new Poolinghttpclientconnectionmanager ();

Connmgr.setmaxtotal (500);//Set the maximum number of connections for the entire connection pool depending on your scenario
Connmgr.setdefaultmaxperroute (Connmgr.getmaxtotal ());

Requestconfig.builder Configbuilder = Requestconfig.custom ();

Configbuilder.setconnecttimeout (1000);//Set maximum number of connections

Configbuilder.setsockettimeout (1000);//Set service-side connection timeout

Configbuilder.setconnectionrequesttimeout (2000);//Set Request connection timeout

Configbuilder.setstaleconnectioncheckenabled (TRUE);
Requestconfig = Configbuilder.build ();

}

Load initialization configuration information to get a HttpClient connection object

Closeablehttpclient httpClient = Httpclients.custom (). Setsslsocketfactory (Createsslconnsocketfactory ()). Setconnectionmanager (connmgr). Setdefaultrequestconfig (Requestconfig)
. Setretryhandler (Httprequestretryhandler). build ();

The rest of the code is the same, the Get,post parameter is self-defined.

About HttpClient's learning experience, request parameter Chinese garbled problem

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.