Java Client initiates HTTP request 2

Source: Internet
Author: User

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

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.