Send Post&Get with Scala

/ 默认分类 / 没有评论 / 34浏览
object test {
    def main(args: Array[String]): Unit = {
    // **Post** 
        val url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
        val httpPost = new HttpPost(url)
        val params = new util.ArrayList[BasicNameValuePair]
        params.add(new BasicNameValuePair("api_key",""))
        params.add(new BasicNameValuePair("api_secret",""))
        params.add(new BasicNameValuePair("image_url", ""))
        httpPost.setEntity(new UrlEncodedFormEntity(params))
        val response=new DefaultHttpClient().execute(httpPost)
        if (response.getStatusLine.getStatusCode == 200) {
            val result = EntityUtils.toString(response.getEntity)
            println(result)
        }
// **Get**
            val api_key = ""
            val api_secret = ""
            val url2 = s"http://api-cn.faceplusplus.com/facepp/v3/${api_key}/${api_secret}"
            val request = new HttpGet(url)
            val httpClient = new DefaultHttpClient()
            val response = httpClient.execute(request)
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                val result = EntityUtils.toString(response.getEntity(), "utf-8")
            }
    }