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