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 1 year has passed since last update.

メモ: Java 11でのHTTP(GET)リクエスト

Posted at

超簡易にHTTPGETリクエストを投げるコードを書き、動作確認したのでメモ。

import java.net.http.*;
import java.net.URI;
import java.io.IOException;

class Main {
    public static void main(String[] args) {
       // HttpClient生成
      HttpClient cli = HttpClient.newHttpClient();
      // HttpRequest生成
      HttpRequest req = HttpRequest.newBuilder()
      .uri(URI.create("http://www.google.com"))
      .GET()
      .build();
      // リクエスト送信
      try {
        HttpResponse<String> ress = cli.send(req, HttpResponse.BodyHandlers.ofString());
        System.out.println("ステータスコード: " + ress.statusCode());
        System.out.println(ress.body());
      } catch(Exception e) {
        System.out.println(e);
      }

    }
  }

ステータスコード301について

リクエストURLを"http://google.com" (wwwなし)でリクエストを投げた場合、ステータスコード: 301 が返ってきた。  

によると、

リクエストされたリソースが Location ヘッダーで示された URL へ完全に移動したことを示します。

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?