The relevant jar packages need to be introduced based on \httpcomponents-client-4.5.5 as follows:
You must import Commons-logging-1.2.jar, otherwise you will be prompted
JSON API Interface Address:
https://www.bejson.com/knownjson/webInterface/
This example uses the interface on Baidu
Test code:
1 ImportOrg.apache.http.HttpStatus;2 ImportOrg.apache.http.client.config.RequestConfig;3 ImportOrg.apache.http.client.methods.CloseableHttpResponse;4 ImportOrg.apache.http.client.methods.HttpGet;5 ImportOrg.apache.http.client.methods.HttpPost;6 Importorg.apache.http.entity.StringEntity;7 Importorg.apache.http.impl.client.CloseableHttpClient;8 Importorg.apache.http.impl.client.HttpClients;9 Importorg.apache.http.util.EntityUtils;Ten One A Public classHttpservletutil { - Public Static voidMain (String args[])throwsexception{ - theHttpservletutil Httpservletutil =Newhttpservletutil (); -String url = "Http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?"; -String params = "scope=103&format=json&appid=379020&bk_key= keyword &bk_length=600"; -String s =httpservletutil.dopost (params, url); + System.out.println (s); - } + A at Public StaticString DoPost (string params, string url)throwsException { -String result =NULL; -Closeablehttpclient httpclient =Httpclients.createdefault (); -Requestconfig requestconfig = Requestconfig.custom (). SetSocketTimeout (). Setconnecttimeout (5000). Build (); -HttpPost HttpPost =Newhttppost (URL); -stringentity entity =NewStringentity (Params.tostring (), "Utf-8"); in httppost.setentity (entity); - //set the request and transmit timeout times to Httppost.setconfig (requestconfig); +Closeablehttpresponse Httpresp =Httpclient.execute (httppost); - Try { the intStatusCode =httpresp.getstatusline (). Getstatuscode (); * //judgment is enough to ask for success $ if(StatusCode = =HTTPSTATUS.SC_OK) {Panax NotoginsengSystem.out.println ("Status code:" +statusCode); -SYSTEM.OUT.PRINTLN ("Request succeeded!")); the //get the returned data +result = Entityutils.tostring (Httpresp.getentity (), "UTF-8"); A}Else { theSystem.out.println ("Status code:" ++httpresp.getstatusline (). Getstatuscode ()); -SYSTEM.OUT.PRINTLN ("HttpPost mode request failed!")); $ } $}finally { - httpresp.close (); - httpclient.close (); the } - returnresult;Wuyi } the - /*Public static string doget (string url) throws exception{ Wu String result = null; - httpclient = Httpclients.createdefault (); About httpget httpget = new HttpGet (URL); $ //Set request and transmit timeout time - //httpget.setconfig (requestconfig); - closeablehttpresponse httpresp = Httpclient.execute (httpget); - try { A int statusCode = Httpresp.getstatusline (). Getstatuscode (); + //judgment is enough to request success the if (StatusCode = = HTTPSTATUS.SC_OK) { - System.out.println ("Status code:" + statusCode); $ System.out.println ("Request succeeded!"); the //Get the returned data the result = entityutils.tostring (Httpresp.getentity (), "UTF-8"); the } else { the System.out.println ("Status code:" - + httpresp.getstatusline (). Getstatuscode ()); in System.out.println ("HttpGet mode request failed!"); the } the } finally { About httpresp.close (); the httpclient.close (); the } the return result; + }*/ -}View Code
After the request succeeds, the following
HttpClient Request JSON data