HttpClient Calling Method

Source: Internet
Author: User

/** * GET request  *  * @param url * Request URL, the parameters are spelled in the request string  */Public static string get (string url) {String responsecontent = null;//Create a default HttpClient instance.closeablehttpclient httpclient = Httpclients.createdefault ();closeablehttpresponse response = null;httpget httpget = new HttpGet (URL);Logger.getrootlogger (). info ("--------------------------------------------");Logger.getrootlogger (). info ("GET" + Httpget.geturi ());//Execute GET request.try {response = Httpclient.execute (httpget);httpentity entity = response.getentity ();//Print response statusLogger.getrootlogger (). info (Response.getstatusline ());if (entity! = null) {responsecontent = entityutils.tostring (entity);//Print response contentLogger.getrootlogger (). info ("Response content:" + responsecontent);Logger.getrootlogger (). info ("--------------------------------------------"); }} catch (Exception e) {e.printstacktrace ();} finally {try {httpclient.close ();response.close ();} catch (IOException e) {e.printstacktrace (); } }return responsecontent;  }  /**  *  * @param url * @param parammap  */Public static string post (string URL, map<string, string> parammap) {String responsecontent = null;//Create a default HttpClient instance.closeablehttpclient httpclient = Httpclients.createdefault ();//Create HttpPostHttpPost httppost = new HttpPost (URL);Logger.getrootlogger (). info ("--------------------------------------------");Logger.getrootlogger (). info ("POST" + Httppost.geturi ());//Create parameter queuelist<basicnamevaluepair> formparams = new arraylist<basicnamevaluepair> ();if (parammap! = null) {set<string> key = Parammap.keyset ();For (iterator<string> it = Key.iterator (); It.hasnext ();) {String paramkey = (string) it.next ();Formparams.add (New Basicnamevaluepair (Paramkey, Parammap. Get (Paramkey)));Logger.getrootlogger (). info (paramkey+ "=" +parammap.get (paramkey)); } } urlencodedformentity uefentity;try {uefentity = new Urlencodedformentity (formparams, "UTF-8");httppost.setentity (uefentity);closeablehttpresponse response = Httpclient.execute (httppost);try {httpentity entity = response.getentity ();Logger.getrootlogger (). info (Response.getstatusline ());if (entity! = null) {responsecontent = entityutils.tostring (Entity, "UTF-8");Logger.getrootlogger (). info ("Response content:" + responsecontent);Logger.getrootlogger (). info ("--------------------------------------------"); }} finally {response.close (); }} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (Unsupportedencodingexception E1) {e1.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {//close connection, release resourcestry {httpclient.close ();} catch (IOException e) {e.printstacktrace (); } }return responsecontent; }

HttpClient Calling Method

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.