JAVA EE HTTPクライアント

HttpURLConnectionクラス

GET

import java.net.URL;
import java.net.HttpURLConnection;
public class myjava
{
 public static void main (String[] args)
 {

 URL url = new URL("https://office-yone.com/" + args[0]);
 HttpURLConnection cn = (HttpURLConnection) url.openConnection();
 cn.setRequestMethod("GET");
  
 if (cn.getResponseCode() == HttpURLConnection.HTTP_OK)
 {
  try (InputStreamReader input = new InputStreamReader(cn.getInputStream(), StandardCharsets.UTF_8);
  BufferedReader reader = new BufferedReader(input)) {
   String line;
   while ((line = reader.readLine()) != null) {
    System.out.println(line);
   }
  }

 }
 cn.disconnect();
 }
}

POST

import java.net.URL;
import java.net.HttpURLConnection;
public class myjava
{
 public static void main (String[] args)
 {

 
  URL url = new URL("https://office-yone.com/");
  HttpURLConnection cn = (HttpURLConnection) url.openConnection();
  cn.setDoOutput(true);
  cn.setRequestMethod("POST");
  
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(cn.getOutputStream(),StandardCharsets.UTF_8))
  writer.write(args[0]);
  writer.flush();
  
  if (cn.getResponseCode() == HttpURLConnection.HTTP_OK) {
   try (InputStreamReader input = new InputStreamReader(cn.getInputStream(),StandardCharsets.UTF_8);
   BufferedReader reader = new BufferedReader(input)) {
    String line;
    while ((line = reader.readLine()) != null) {
     System.out.println(line);
    }
   }

  }
  cn.disconnect();
 }
}

CloseableHttpClientクラス

GET

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
public class myjava
{
 public static void main (String[] args)
 {

  try (CloseableHttpClient client = HttpClients.createDefault()) {
  try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
  
   HttpGet method = new HttpGet("https://office-yone.com/" + args[0]);
   
   try (CloseableHttpResponse res = client.execute(method)) {
    if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
     HttpEntity entity = res.getEntity();
     System.out.println(EntityUtils.toString(entity,StandardCharsets.UTF_8));

    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

POST

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
public class myjava
{
 public static void main (String[] args)
 {

  try (CloseableHttpClient client = HttpClients.createDefault()) {
  try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
  
   HttpPost method = new HttpPost("https://office-yone.com/");
   StringBuilder builder = new StringBuilder();
   builder.append(args[0]);
   
   method.setEntity(new StringEntity(builder.toString(),StandardCharsets.UTF_8));
   
   try (CloseableHttpResponse res = client.execute(postMethod)) {
    if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
     HttpEntity entity = res.getEntity();
     System.out.println(EntityUtils.toString(entity,StandardCharsets.UTF_8));

    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}