Client Request method One, if the data type is x-www-form-urlencoded
The JSON jar package used is gradle (' com.alibaba:fastjson:1.2.38 ',)
Then the corresponding code fragment is:
Val url = "Http://www.unionpayintl.com/cardholderServ/serviceCenter/rate/search"val Params= mutablelistof<namevaluepair>() Val pair= Basicnamevaluepair ("Curdate", SimpleDateFormat ("Yyyy-mm-dd"). Format (Date ()))Params.add (pair) Val client=Httpclients.createdefault () Val httppost=httppost (URL)Try{httppost.entity= Urlencodedformentity (params, "UTF-8") Httppost.setheader ("Content-type", "application/x-www-form-urlencoded") Val Response=Client.execute (HttpPost) Val StatusCode=Response.statusLine.statusCodeif(StatusCode = = 200) {Val entity=response.entity Val S=entityutils.tostring (Entity) println (Jsonobject.parseobject (s) )returnJsonobject.parseobject (s). Getdoublevalue ("exchangerate") } } Catch(e:ioexception) {e.printstacktrace ()}
If the requested data is in JSON format, the
Val url = "Http://www.baidu.com?" Id=12&name=3 " try { = httputils.get (URL) = Jsonobject.parseobject ( Result) catch (e:exception) { e.printstacktrace () }
Java Client initiates HTTP request 2