LoginSignup
1
0

More than 1 year has passed since last update.

Scala の HttpClient の使い方 (Get)

Last updated at Posted at 2018-03-03

次の Java プログラムを scala で書いてみました。
Java の HttpClient の使い方 (Get)

Http_get.scala
// -------------------------------------------------------------------
//  Http_get.scala
//
//                  Jun/12/2021
//
// -------------------------------------------------------------------
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import org.apache.http.HttpStatus
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils


// -------------------------------------------------------------------
object Http_client {

def runSample() = {
    System.err.println ("*** runSample *** start ***")

    var charset = StandardCharsets.UTF_8

    var httpclient = HttpClients.createDefault()

    var request = new HttpGet("https://httpbin.org/get")

    println ("requestの実行 「" + request.getRequestLine() + "」")

    var response:CloseableHttpResponse = null


    try {
        response = httpclient.execute(request)

        var status = response.getStatusLine().getStatusCode()
        println("HTTPステータス:" + status)

        if (status == HttpStatus.SC_OK){                
            var responseData = 
                    EntityUtils.toString(response.getEntity(),charset)              
            println(responseData)
            }
        } catch {
            case e: ClientProtocolException => e.printStackTrace()
            case e: UnsupportedEncodingException => e.printStackTrace()
            case e: IOException => e.printStackTrace()

        } finally {

            try {
                if (response != null) {
                    response.close()
                }
                if (httpclient != null) {
                    httpclient.close()
                }
            } catch {
                case e: IOException => e.printStackTrace()
            }

        }

        System.err.println ("*** runSample *** end ***")
    }
}


// -------------------------------------------------------------------
object Http_get {

def main(args: Array[String])
{
    System.err.println ("*** 開始 ***")

    Http_client.runSample()

    System.err.println ("*** 終了 ***")
}
}

// -------------------------------------------------------------------
Makefile
LIB=/usr/share/java
HTTPCLIENT_JAR=.:$(LIB)/httpclient-4.5.13.jar:$(LIB)/httpcore-4.4.14.jar
Http_get.class: Http_get.scala
    scalac -cp $(HTTPCLIENT_JAR) Http_get.scala
clean:
    rm -f *.class

実行コマンド

LIB=/usr/share/java
HTTPCLIENT_JAR=$LIB/httpclient-4.5.13.jar:$LIB/httpcore-4.4.14.jar:$LIB/commons-logging-1.2.jar
export CLASSPATH=.:$HTTPCLIENT_JAR
#
scala Http_get
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0