How to use Java HttpClient (Get)

I referred to the next page. Java HTTP Communication Sample (HttpClient)

Download and unzip the following files. httpcomponents-client-4.5.6-bin.tar.gz

// -----------------------------------------------------------------------

// -----------------------------------------------------------------------
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 sample
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);
			("Execution of request "" + request.getRequestLine() + "」");
		CloseableHttpResponse response = null;

		try {
			response = httpclient.execute(request);
			int status = response.getStatusLine().getStatusCode();
			System.out.println("HTTP status:" + status);
			//HTTP status:200
			if (status == HttpStatus.SC_OK){				
				res = EntityUtils.toString(response.getEntity(),charset);				
		} catch (ClientProtocolException e) {
		} catch (UnsupportedEncodingException e) {
		} catch (IOException e) {
		} finally {
			try {
				if (response != null) {
				if (httpclient != null) {
			} catch (IOException e) {

		return res;

// -----------------------------------------------------------------------
public class Http_get
	public static void main(String[] args)
		String uri = "";

		String res = Uri_get.uri_get_proc(uri);

// -----------------------------------------------------------------------


	javac -cp $(HTTPCLIENT_JAR)
	rm -f *.class

Execution command

java -cp $HTTPCLIENT_JAR Http_get

Execution result

Execution of request "GET HTTP/1.1」
HTTP status:200
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip,deflate", 
    "Connection": "close", 
    "Host": "", 
    "User-Agent": "Apache-HttpClient/4.5.6 (Java/10.0.2)"
  "origin": "", 
  "url": ""

If you do the same with curl

$ curl
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "close", 
    "Host": "", 
    "User-Agent": "curl/7.61.1"
  "origin": "", 
  "url": ""

Recommended Posts

How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to get the current directory
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to get the date in java
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes