Groovy の HttpClient の使い方 (Post)

次の Java プログラムを groovy で書いてみました。
[Java の HttpClient の使い方 (Post)](https://qiita.com/ekzemplaro/items/4cc3d228ad1514ab36a7)

```groovy:http_post.groovy
// -----------------------------------------------------------------------
/*
	http_post.groovy


					Jun/12/2021
*/
// -----------------------------------------------------------------------
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.ArrayList
import java.util.List
import org.apache.http.HttpStatus
import org.apache.http.NameValuePair
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils

/**
 * HttpClientsのサンプル
 */
class Http_client {

	void runSample() {
		Charset charset = StandardCharsets.UTF_8

		CloseableHttpClient httpclient = HttpClients.createDefault()
		
		HttpPost request = new HttpPost("https://httpbin.org/post")		
		List<NameValuePair> requestParams = new ArrayList<>()
		requestParams.add(new BasicNameValuePair("user","jiro"))
		requestParams.add(new BasicNameValuePair("password","123456"))
		
		println ("requestの実行　「" + request.getRequestLine() + "」")
		
		CloseableHttpResponse response = null

		try {
			request.setEntity(new UrlEncodedFormEntity(requestParams))
			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_post {

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

	Http_client ss = new Http_client()
	ss.runSample()

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

// -----------------------------------------------------------------------
```

```text:Makefile
LIB=/usr/share/java
HTTPCLIENT_JAR=.:$(LIB)/httpclient-4.5.13.jar:$(LIB)/httpcore-4.4.14.jar
http_post.class: http_post.groovy
	groovyc -cp $(HTTPCLIENT_JAR) http_post.groovy
clean:
	rm -f *.class
```

実行コマンド

```bash
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_post
```