0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Groovy の HttpClient の使い方 (Get)

Last updated at Posted at 2018-02-27

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

http_get.groovy
// -------------------------------------------------------------------
//	http_get.groovy
//
//					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

// -------------------------------------------------------------------
class Http_client {

	void runSample() {
		System.err.println ("*** runSample *** start ***")
		Charset charset = StandardCharsets.UTF_8
		
		CloseableHttpClient httpclient = HttpClients.createDefault()
		HttpGet request = new HttpGet("https://httpbin.org/get")
		
		println	("requestの実行 「" + request.getRequestLine() + "」")
		
		CloseableHttpResponse response = null

		try {
			response = httpclient.execute(request)
			
			int status = response.getStatusLine().getStatusCode()
			println("HTTPステータス:" + status)
			//HTTPステータス:200
			
			if (status == HttpStatus.SC_OK){				
				String responseData = 
					EntityUtils.toString(response.getEntity(),charset)				
				println(responseData)
				//取得したデータが表示される
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace()
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace()
		} catch (IOException e) {
			e.printStackTrace()
		} finally {
			try {
				if (response != null) {
					response.close()
				}
				if (httpclient != null) {
					httpclient.close()
				}
			} catch (IOException e) {
				e.printStackTrace()
			}
		}
	}
}

// -------------------------------------------------------------------
public class Http_get {

public static void main(String[] args)
{
	System.err.println ("*** 開始 ***")

	Http_client ss = new Http_client()

	ss.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.groovy
	groovyc -cp $(HTTPCLIENT_JAR) http_get.groovy
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
GROOVY_ALL_JAR=/usr/share/groovy/embeddable/groovy-all-2.4.21.jar
export CLASSPATH=.:$GROOVY_ALL_JAR:$HTTPCLIENT_JAR
#
java Http_get
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?