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 の使い方 (Post)

Last updated at Posted at 2018-02-27

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

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 ("*** 終了 ***")
}
}

// -----------------------------------------------------------------------
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

実行コマンド

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
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?