7
8

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.

Java の HttpClient の使い方 (Get)

Last updated at Posted at 2018-02-27

次のページを参考にしました。
Java HTTP通信のサンプル(HttpClient)

Ubuntu 21.04 で確認しました。

Http_get.java
// -----------------------------------------------------------------------
/*
	Http_get.java

				Jul/30/2020
*/
// -----------------------------------------------------------------------
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;

/**
 * HttpClientsのサンプル
 */
class Uri_get
{
	static String uri_get_proc(String uri)
		{
		String res = "";
		Charset charset = StandardCharsets.UTF_8;
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet request = new HttpGet(uri);
		
		System.out.println
			("requestの実行 「" + request.getRequestLine() + "」");
		
		CloseableHttpResponse response = null;

		try {
			response = httpclient.execute(request);
			
			int status = response.getStatusLine().getStatusCode();
			System.out.println("HTTPステータス:" + status);
			//HTTPステータス:200
			
			if (status == HttpStatus.SC_OK){				
				res = EntityUtils.toString(response.getEntity(),charset);				
			}
		} 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();
			}
		}

		return res;
	}
}

// -----------------------------------------------------------------------
public class Http_get
{
	public static void main(String[] args)
	{
		String uri = "https://httpbin.org/get";

		String res = Uri_get.uri_get_proc(uri);
		System.out.println(res);
	}
}

// -----------------------------------------------------------------------
Makefile
LIB=/usr/share/java
HTTPCLIENT_JAR=.:$(LIB)/httpclient-4.5.13.jar:$(LIB)/httpcore-4.4.14.jar
Http_get.class: Http_get.java
	javac -cp $(HTTPCLIENT_JAR) Http_get.java
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
#
java -cp $HTTPCLIENT_JAR Http_get

実行結果

requestの実行 「GET https://httpbin.org/get HTTP/1.1」
HTTPステータス:200
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "Apache-HttpClient/4.5.13 (Java/11.0.11)", 
    "X-Amzn-Trace-Id": "Root=1-5f226ffa-accebcf2bde7af40b3507628"
  }, 
  "origin": "219.126.139.62", 
  "url": "https://httpbin.org/get"
}

同じことを curl で実行すると

$ curl https://httpbin.org/get
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.68.0", 
    "X-Amzn-Trace-Id": "Root=1-5f227030-f41530438d1cfcefe66f5c05"
  }, 
  "origin": "219.126.139.62", 
  "url": "https://httpbin.org/get"
}
7
8
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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?