HttpClient POST Request

Source: Internet
Author: User

DoPost (NULL, "https://www.baidu.com/");

/**
* Accessing the database and returning the JSON data string
*
* @param params
* Parameters passed to the server side
* @param URL
* @return
* @throws Exception
*/
public static string DoPost (list<namevaluepair> params, string url)
Throws Exception {
String result = null;
Get HttpClient Object
HttpClient HttpClient = new Defaulthttpclient ();
New HttpPost Object
HttpPost HttpPost = new HttpPost (URL);
if (params! = null) {
Set character sets
httpentity entity = new Urlencodedformentity (params, HTTP. UTF_8);
Set parameter Entities
Httppost.setentity (entity);
}
Connection timed out
Httpclient.getparams (). Setparameter (
Coreconnectionpnames.connection_timeout, 3000);
Request timed out
Httpclient.getparams (). Setparameter (Coreconnectionpnames.so_timeout,
3000);
Get HttpResponse Instance
HttpResponse Httpresp = Httpclient.execute (HttpPost);
Judgment is enough to ask for success
if (Httpresp.getstatusline (). Getstatuscode () = = 200) {
Get the returned data
result = Entityutils.tostring (Httpresp.getentity (), "UTF-8");
} else {
result = NULL;
}
return result;

}

HttpClient POST Request

Related Article

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.