Java HttpResponse create

Description

Java HttpResponse create

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {

    public static void main(String[] args) {

        HttpResponse r1;// ww  w. ja  va2 s  .c o  m
        try {
            r1 = HttpRequest.create(new URI("https://www.demo2s.com/"))
                    .GET()
                    .response();

            int responseCode = r1.statusCode();
            if(responseCode == 200){
                System.out.println(r1.body(HttpResponse.asString()));
            }

         } catch (URISyntaxException|IOException|InterruptedException ex) {
            ex.printStackTrace();
        }
        
    }

}



PreviousNext

Related